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

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

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

3 comments

0

Is this a local script?

0

No, does it have to be?

0

No, I was just wondering incase you were trying to access ServerStorage from a local script, which you can't do.

2 answers

✓ Accepted answer
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

2 comments

0

Thank you, this works great!

0

No problem

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. ;)

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