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

How to deal damage to all players?

Asked by
Lazarix9 245 Moderation Voter
4 years ago

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 ^^

0
Why are you checking if character is true, that will always be false User#24403 69 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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!

0
I modified your char == true by dropping the true check due to conflicting thought on other users. I don't have time to test others so I made the change. alphawolvess 1784 — 4y
0
This works, thank you. Lazarix9 245 — 4y
Ad
Log in to vote
1
Answered by
Prestory 1395 Moderation Voter
4 years ago
Edited 4 years ago

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)
0
Doing if char then and if char == true then has no difference besides keystroke. The issue is char is never updated, therefore char is always false because char is created too soon when it does not exists alphawolvess 1784 — 4y
0
No, characters are never equal to true. player.Character will either evaluate to a userdata or nil. User#24403 69 — 4y
0
This did not work. Thank you for taking your time to reply anyways, I appreciate it. Lazarix9 245 — 4y
0
NP Prestory 1395 — 4y

Answer this question