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

How do I make a script that changes the max health of specific players?

Asked by 4 years ago
Edited 4 years ago

I'm trying to create a script that allows me to modify both the maxhealth and health of people in a group, via their username, or on a certain team. I've tried numerous variations, but I've been unable to make it work for specific players, only for the whole game.

local player = game.Players.LocalPlayer

if player.Name == "julianhak06" then

game.Players.PlayerAdded:Connect(function(plr)

    plr.CharacterAdded:connect(function(Char)

        if Char:FindFirstChild("Humanoid") then

            Char.Humanoid.MaxHealth = 500
            Char.Humanoid.Health = 500
        end
    end)
end)

end

That's my code, please let me know what I can fix, I'm very new to scripting, so I have a lot to learn

EDIT: Not sure why some of my code is out of the correct format

1 answer

Log in to vote
1
Answered by
NotedAPI 810 Moderation Voter
4 years ago
Edited 4 years ago

Hello Julianhak06,

I recommend using a normal script so the changes can replicate to the server, as well as using UserIDs instead of their name, so if they change their name, you won't have to update the script. In my example, we will be using a table and using a for loop to get the contents in that table.

local Players = game:GetService('Players')

local SetHealth = 500 --// The amount their health will be set to

local AllowedIDs = { --// The list of IDs that will have their health changed
    1,
    2,
}

Players.PlayerAdded:Connect(function(player)
    local canSetHealth = false
    for index, id in pairs(AllowedIDs) do
        -- // This will loop through the table and get each ID
        if player.userId == id then
            canSetHealth = true --// This sets the canSetHealth variable to true if the userId matches an id in the table
        end
    end

    if canSetHealth then -- // If canSetHealth is true, then it will set their health
        player.CharacterAdded:Connect(function(character)
            character:WaitForChild('Humanoid')
            character.Humanoid.MaxHealth = SetHealth
            character.Humanoid.Health = SetHealth
        end)
    end
end)

GetRankInGroup (https://developer.roblox.com/en-us/api-reference/function/Player/GetRankInGroup)

local Players = game:GetService('Players')

local GroupID = GROUPID
local RankAllowed = RANKID

Players.PlayerAdded:Connect(function(player)
    if player:GetRankInGroup(GroupID) >= RankAllowed then
        player.CharacterAdded:Connect(function(character)
            character:WaitForChild('Humanoid')
            character.Humanoid.MaxHealth = SetHealth
            character.Humanoid.Health = SetHealth
        end)
    end
end)

IsInGroup (https://developer.roblox.com/en-us/api-reference/function/Player/IsInGroup)

local Players = game:GetService('Players')

local GroupID = GROUPID

Players.PlayerAdded:Connect(function(player)
    if player:IsInGroup(GroupID) then
        player.CharacterAdded:Connect(function(character)
            character:WaitForChild('Humanoid')
            character.Humanoid.MaxHealth = SetHealth
            character.Humanoid.Health = SetHealth
        end)
    end
end)
0
Works like a charm, thanks! But how could I set it for a group, for example? julianhak06 12 — 4y
0
You could use the Player:IsInGroup() or Player:GetRankInGroup() method. I will edit my answer with both in a few. NotedAPI 810 — 4y
0
Thanks! Have a good day. julianhak06 12 — 4y
0
You as well! :D NotedAPI 810 — 4y
Ad

Answer this question