Jump to content

[SOLVED] for ... do


myonlake

Recommended Posts

Hello everyone,

I am attempting to make a code that moves all vehicles and players away from the marker when I have executed the command.

If you know what "Fus Ro Dah" means, you should know what I am trying to catch here.

Server

function fusRoDah(player, cmd) 
    if hasObjectPermissionTo(player, "function.banPlayer") then 
        local px, py, pz = getElementPosition(player) 
        outputChatBox("Fus Ro Dah!", root, 255, 0, 0, false) 
        triggerClientEvent("onFusCommand", root, px, py, pz) 
        marker = createMarker(px, py, pz, "cylinder", 150, 255, 0, 0, 255) 
        setTimer(function() destroyElement(marker) end, 1000, 1) 
         
        for ipl,pla in ipairs(getElementsByType("player")) do 
            if not isElementWithinMarker(pla, marker) then end 
            local x, y, z = getElementPosition(pla) 
            setElementPosition(pla, x + 50, y + 50, z + 1) 
        end 
         
        for ive,veh in ipairs(getElementsByType("vehicle")) do 
            if not isElementWithinMarker(veh, marker) then end 
            local x, y, z = getElementPosition(veh) 
            setElementPosition(veh, x + 50, y + 50, z + 1) 
        end 
    end 
end 
addCommandHandler("fus", fusRoDah) 
 

Client

local root = getRootElement() 
local thisRoot = getResourceRootElement(getThisResource()) 
  
function playFusRoDah(px, py, pz) 
    local sound = playSound3D("fusrodah.wav", px, py, pz, false) 
end 
addEvent("onFusCommand", true) 
addEventHandler("onFusCommand", root, playFusRoDah) 
 

So, it plays the voice and everything correctly. But it doesn't move the players or vehicles away from the marker. It doesn't output any errors or anything. I assume it's just that it checks the for ... do stuff and if one of them is invalid, it won't execute my function I am trying to do here.

Edited by myonlake
Link to comment

I have removed the annoying else returns now.

I am not sure have you really understood the problem; it possibly ends the function as it detects a player or a vehicle that is not in the marker, meaning that all players and vehicles should be in the marker to let the script work.

EDIT:

I have fixed the problem by making a check if the element is in the marker. Check the updated code.

Link to comment
marker = createMarker(px, py, pz, "cylinder", 150, 255, 0, 0, 255) 
setTimer(function() destroyElement(marker) end, 1000, 1) 

Instead of creating a marker and destroying it a second after it has been created, I would just add a colshape to the player when he joins. If you know that he is gonna use "Fus Ro Dah" when he is connected, the col could just be there all the time.

It could be something like:

col = {} 
  
function fusRoDahMe() 
local x,y,z = getElementPosition(source) 
col[source] = createColCircle(x,y,150) 
attachElements(col[source], source) 
end 
addEventHandler("onPlayerSpawn", root, fusRoDahMe) 

Like this you can just check if players/vehs are in the attached colshape everytime you need it.

Edited by Guest
Link to comment
marker = createMarker(px, py, pz, "cylinder", 150, 255, 0, 0, 255) 
setTimer(function() destroyElement(marker) end, 1000, 1) 

Instead of creating a marker and destroying it a second after it has been created, I would just add a colshape to the player when he joins. If you know that he is gonna use "Fus Ro Dah" when he is connected, the col could just be there all the time.

It could be something like:

  
col = {} 
  
function fusRoDahMe() 
local x,y,z = getElementPosition(source) 
col[source] = createColCircle(x,y,150) 
attachElements(col[source], source) 
end 
addEventHandler("onPlayerSpawn", root, fusRoDahMe) 
  
  

Like this you can just check if players/vehs are in the attached colshape everytime you need it.

Thanks for the suggestion, I thought about it aswell but decided to work on a little easier first.

I am changing it to colshapes now ;)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...