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

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

Asked by 7 years ago
Edited 7 years ago

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

Log in to vote
0
Answered by 7 years ago

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!

Ad

Answer this question