Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

4 comments

0

Works like a charm, thanks! But how could I set it for a group, for example?

0

You could use the Player:IsInGroup() or Player:GetRankInGroup() method. I will edit my answer with both in a few.

0

Thanks! Have a good day.

0

You as well! :D

This discussion is preserved as it was. New answers, comments, and votes are closed.