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

Im having issues with a script made to change the players health can someone assist me?

The script is in a normal script and in workspace

game.Players.PlayerAdded:Connect(function(Player)
	local hi = Player.Character
	local humanoid = hi:FindFirstChild("Humanoid")
	local leaderpoints = Player:FindFirstChild("leaderstats")
	
	humanoid.MaxHealth = leaderpoints.Durability.Value
	print(leaderpoints.Durability.Value)
	
end)

13 comments

0

what is da problem

0

also what u r doing is not rly efficient

0

also there is plausibility that the humanoid has not loaded at the time u were defining it so u might have been asking for a nil the whole time

0

i think thats the issue thank you

0

Wait, nevermind. I read it wrong. facepalm

0

lol ok

0

oof how do i fix this anyways ive been at it all day

0

Use local humanoid = hi:WaitForChild("Humanoid"). If it doesn't work comment it didn't and I'll look into it.

0

Also, are you wanting it so that the humanoid's maxhealth is displayed on the leaderboard?

0

no its so that the value on the leaderboard is the maxhealth also i will try what you said

0

If you want it to do that, in my answer below, switch durability.Value and humanoid.MaxHealth around.

0

alright

0

any errors?

1 answer

✓ Accepted answer

Okay. Let's break it down. After you wait for the player to be added you're going to want to create your leaderstats. In this case, I'm going to make it a folder. Then we're going to want to create an IntValue inside of it called Durability. Then, we're going to find the character of the player. The way it works is it's going to wait for the player to load into the game, we're using Player.Name so it knows we're looking for a model with the name of your player. Then we're going to set the value of the leaderstats to the player's maxhealth.

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = Player
	leaderstats.Name = "leaderstats"
	local durability = Instance.new("IntValue")
	durability.Parent = leaderstats
	durability.Name = "Durability"
    local humanoid = game.Workspace:WaitForChild(Player.Name).Humanoid
    local leaderpoints = Player:FindFirstChild("leaderstats")

	durability.Value = humanoid.MaxHealth
    print(leaderpoints.Durability.Value)

end)

Edit:

game.Players.PlayerAdded:Connect(function(Player)
    local humanoid = game.Workspace:WaitForChild(Player.Name).Humanoid

    local leaderpoints = Player:FindFirstChild("leaderstats")

    humanoid.MaxHealth = leaderpoints.Durability.Value
    print(leaderpoints.Durability.Value)

end)

11 comments

0

Sorry, I'm bad at explaining, but there you go. If it worked, please click the accept answer button.

0

i already have a leaderboard with the value Durability but i will try this

0

You may need to tweak it a bit to fit the names of your leaderstats and where they are in the game. The only error I think you made was the humanoid part. So just put that into your script and it should work.

0

You should use the edited version if you already have the leaderstats. I put it under the other code.

0

thank you it works how do i accept the answer?

0

also is there a way to have it constantly update? the durability value changes and the health changes with it?

0

use the .Changed event of IntValue

0

thanks

0

where exactly would i put the .Changed? i tried leaderpoints.Durability.Changed:Connect(function() humanoid.MaxHealth = leaderpoints.Durability.Value but it doesnt work

0

Maybe instead of "game.Workspace.WaitForChild(Player.Name).Humanoid", do "(Player.Character or Player.CharacterAdded:Wait()).Humanoid", to avoid issues where some player has the same name as some other object.

0

ok

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