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:
01 | --Variables |
02 | local bulb = script.Parent |
03 | local stageone = bulb.Stage 1 |
04 | local StarterGUI = game.StarterGui |
05 | local DmgGUI = StarterGUI.BulbDmgGUI |
06 | local Players = game:GetService( 'Players' ) |
07 | --Code |
08 | Players.PlayerAdded:Connect( function (plr) |
09 |
10 | local char = plr.Character |
11 | if char = = true then |
12 | local humanoid = char:FindFirstChild( 'Humanoid' ) |
13 |
14 | while bulb.Stage 1. Value = = true do |
15 | print ( 'hi' ) |
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.
01 | --Variables |
02 | local bulb = script.Parent |
03 | local stageone = bulb.Stage 1 |
04 | local StarterGUI = game.StarterGui |
05 | local DmgGUI = StarterGUI.BulbDmgGUI |
06 | local Players = game:GetService( 'Players' ) |
07 | --Code |
08 | Players.PlayerAdded:Connect( function (plr) |
09 | plr.CharacterAdded:Wait() |
10 | local char = plr.Character |
11 | if char then |
12 | local humanoid = char:FindFirstChild( 'Humanoid' ) |
13 |
14 | while bulb.Stage 1. Value = = true do |
15 | print ( 'hi' ) |
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.
01 | --Variables |
02 | local bulb = script.Parent |
03 | local stageone = bulb.Stage 1 |
04 | local StarterGUI = game.StarterGui |
05 | local DmgGUI = StarterGUI.BulbDmgGUI |
06 | local Players = game:GetService( 'Players' ) |
07 | --Code |
08 | Players.PlayerAdded:Connect( function (plr) |
09 |
10 | local char = plr.Character |
11 | if char then |
12 | local humanoid = char:FindFirstChild( 'Humanoid' ) |
13 |
14 | while bulb.Stage 1. Value = = true do |
15 | print ( 'hi' ) |