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

Why isn't my "if" statement executing the code?

I am making a game heavily based off of BuildIntoGames', "Guest Defense", and the if statement isn't working so that it can end the wave!

while true do
	if(game.Workspace.Enemyskilled.Value == 8) then
	print("Wave 1 finished")
	randomSong:Stop()
	script.Voice:Play()
	player.PlayerGui.Wave1:Remove()
	end
	end

1 answer

In a while true do loop you must add wait() or else the code will crash causing Studio to crash (possibly)

What I suggest you do is label all the variables;

local randomSong = -- your variable
local V = script:WaitForChild('Voice')
local plr = game.Players.LocalPlayer

remember you can use the player in local scripts!

If that script is only part of your code then, please add the rest in.

Anyway, the script should look like this if you have all your variables;

local variables 
 -- insert all variables
while true do
	wait(1)
   	 if(game.Workspace.Enemyskilled.Value == 8) then
   		 print("Wave 1 finished")
   		 randomSong:Stop() 
    		 script.Voice:Play()
   		 player.PlayerGui.Wave1:Destroy() -- Do not use :Remove() use :Destroy()
    	   end
    end

If you are able to add all variables and make sure this is a local script and all then your good to go!

Anyway, this is basically a model for you to learn on why your script isn't working.

  1. You probably didn't add wait()
  2. you probably got the variables wrong
  3. this is probably a server script, that's why player doesn't work

If you checked through all of those then your code should work

If I helped then please accept answer ;)

Good luck and have fun developing!

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