Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my ifs not executing the functions even if the value is true?

Asked by 3 years ago

This is what i did so far:

if math.random(1,4) == 1 then
    moveforward()
end
if math.random(1,4) == 2 then
        movebackwards()
end
if math.random(1,4) == 3 then
        moveleft()
end
if math.random(1,4) == 4 then
        moveright()
end

But yet when i run the code it won't run even if i add a wait() when i added a wait() it worked once but after that it didnt move after i ran the script again, i even tryed making a math random that loops with a wait(2) yet it still wont move or execute its functions, functions are just a cframe for moving it, and a print to know if it executed or no

while true do
    mr = math.random(1,4)
    wait(2)
end

Heres the code of the math random that loops

0
try using a while wait(2) do instead of just putting wait(2) at the bottom. Dovydas1118 1495 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

Add a wait loop so it constantly checks. If stamens only check once

while wait(3) do

if math.random(1,4) == 1 then
    moveforward()
end
if math.random(1,4) == 2 then
        movebackwards()
end
if math.random(1,4) == 3 then
        moveleft()
end
if math.random(1,4) == 4 then
        moveright()
end

end

Sorry for the bad format, I’m on mobile

0
This makes sense, i thought if was looped and executed just when the statement becomes true back7666 2 — 3y
Ad
Log in to vote
0
Answered by
TNTIsLyfe 152
3 years ago
Edited 3 years ago

I am confused why dont u use elseif, Here is the fixed version for u Ill explain the bugs

while true do wait (1) -- u should add this so it infinitely loops
local Number = math.random(1,4)
if Number == 1 then
    moveforward()
elseif Number == 2 then -- use elseif as it also gives alternate conditions,
        movebackwards()
elseif Number == 3 then
        moveleft()
elseif Number == 4 then
        moveright()
end
end

i am sorry if this wasnt the outcome u were expecting, i didnt kinda understand what u wanted in the script

0
i am trying to make a Artificial intelligcence back7666 2 — 3y

Answer this question