Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

1 comment

0

try using a while wait(2) do instead of just putting wait(2) at the bottom.

2 answers

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

1 comment

0

This makes sense, i thought if was looped and executed just when the statement becomes true

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

1 comment

0

i am trying to make a Artificial intelligcence

This discussion is preserved as it was. New answers, comments, and votes are closed.