When a certain boolean value is equal to true, I want all players in the server to take damage every 5 seconds and a gui to appear for a split second. I have this script, I do not know what's wrong with it and in the output, there are no errors that appear. I tried some other stuff, including for loops but it didn't work, I don't understand them well tho so if you are using for loops, I would appreciate if you explained in detail what stuff does, thanks :3
Here's the script:
--Variables local bulb = script.Parent local stageone = bulb.Stage1 local StarterGUI = game.StarterGui local DmgGUI = StarterGUI.BulbDmgGUI local Players = game:GetService('Players') --Code Players.PlayerAdded:Connect(function(plr) local char = plr.Character if char == true then local humanoid = char:FindFirstChild('Humanoid') while bulb.Stage1.Value == true do print('hi') StarterGUI.BulbDmgGUI.Enabled = true humanoid:TakeDamage(10) wait(.2) StarterGUI.BulbDmgGUI.Enabled = false wait(5) end end end)
This code is all in a Server Script. Thanks again ^^
Your problem is when you create the variable char
, you do so the instant a player joins. At this time, there will never be a character made, yet, so char is set to false. Since the variable is never updated, you never get char to equal true because PlayerAdded
will fire once for that person.
To fix your issue, shown below, we simply need a way to wait for the Character to exists which can be done in several ways. I used a wait on the character added event to pause the code until the character is made.
--Variables local bulb = script.Parent local stageone = bulb.Stage1 local StarterGUI = game.StarterGui local DmgGUI = StarterGUI.BulbDmgGUI local Players = game:GetService('Players') --Code Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Wait() local char = plr.Character if char then local humanoid = char:FindFirstChild('Humanoid') while bulb.Stage1.Value == true do print('hi') StarterGUI.BulbDmgGUI.Enabled = true humanoid:TakeDamage(10) wait(.2) StarterGUI.BulbDmgGUI.Enabled = false wait(5) end end end)
If your code still does not work, an issue may be due to how Roblox loads a player then a character or you are getting nil for humanoid. Feel free to ask another question or reply by comment if so!
Char == true is non existent instead If char then is used like i have used it below.
--Variables local bulb = script.Parent local stageone = bulb.Stage1 local StarterGUI = game.StarterGui local DmgGUI = StarterGUI.BulbDmgGUI local Players = game:GetService('Players') --Code Players.PlayerAdded:Connect(function(plr) local char = plr.Character if char then local humanoid = char:FindFirstChild('Humanoid') while bulb.Stage1.Value == true do print('hi') StarterGUI.BulbDmgGUI.Enabled = true humanoid:TakeDamage(10) wait(.2) StarterGUI.BulbDmgGUI.Enabled = false wait(5) end end end)