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

Issue with a simple if statement script. What error did I make?

Asked by 5 years ago

This code is supposed to have a 20% chance of spawning in a part from server storage and then destroy it 29 seconds later. If it does not spawn the part it is then supposed to wait for 6 seconds before attempting to spawn the part again. I checked to make sure its name in server storage was "Part" and at this point, the only error I am receiving is to close "while true do" with an "end" which would crash my studio. Does anyone know the problem with the script?

while true do
 Bodys = math.random (1,5)
    if Bodys == 5 then 
 local model = game.ServerStorage.Part:Clone()
 model.Parent=game.Workspace
 wait(29)
model:Destroy()
else 
        wait(6)
        end
0
Is this a local script? jackfrost178 242 — 5y
0
No, does it have to be? User#18068 0 — 5y
0
No, I was just wondering incase you were trying to access ServerStorage from a local script, which you can't do. jackfrost178 242 — 5y

2 answers

Log in to vote
0
Answered by
Prestory 1395 Moderation Voter
5 years ago
Edited 5 years ago
while wait() do
Bodys = math.random (1,5)
if Bodys == 5 then 
local model = game.ServerStorage.Part:Clone()
model.Parent=game.Workspace
wait(29)
model:Destroy()
else 
wait(6)
end
end
0
Thank you, this works great! User#18068 0 — 5y
0
No problem Prestory 1395 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

You are missing an "end"; since you have "while true do" and an "if" there should be two "end"s.

Here's your code with a small edit:

while true do
    Bodys = math.random (1,5)
    if Bodys ==5 then
        local model = game.ServerStorage.Part:Clone()
        model.Parent=game.Workspace
        wait(29)
        model:Destroy
    else
        wait(6)
    end
end

If you have any questions or problems please contact me. ;)

Answer this question