MaxHealth IntValue

  1. What do you want to achieve? Keep it simple and clear!
    a: a fix to my issue

  2. What is the issue? Include screenshots / videos if possible!
    a: The issue is that im making a clicker game and each time you click you gain an Intvalue called “MaxHealth” in the leaderstats folder, and i trying to make it so that the MaxHealth value on the player = MaxHealth Intvalue and it isnt working, cann someone help? Btw it is a local script in the startercharacterscripts.

This is the script:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		char.Humanoid.MaxHealth.Value = player.leaderstats.MaxHealth.Value 
	end)
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried a lot of solutions on the devforum and found no answers to my problem

It’s not working because the ‘char’ in your code, so you will have to revise it.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.MaxHealth.Value = player.leaderstats.MaxHealth.Value 
	end)
end)

Hope it helps.

thanks for replying, but I tried it and it did not change anything.

Oh wait, I got it, here’s the revised version:

local Players = game:GetService(“Players”)

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.MaxHealth = player.leaderstats.MaxHealth.Value 
	end)
end)

remove .value in char.Humanoid.MaxHealth.Value
.

This should look like this: Character.Humanoid.MaxHealth = Plr.leaderstats.MaxHealth.Value

You’ll need to detect when the value changes using the Changed event like so:

local Players = game:GetService("Players")

local connections = {}

Players.PlayerAdded:Connect(function(player)
	connections[player] = {}

	connections[player].CharacterAdded = player.CharacterAdded:Connect(function(character)
		connections[player].MaxHealthChanged = player.leaderstats.MaxHealth.Changed:Connect(function(value)
			character.Humanoid.MaxHealth = value
		end)
	end)

	connections[player].CharacterRemoving = player.CharacterRemoving:Connect(function()
		if connections[player].MaxHealthChanged then
			connections[player].MaxHealthChanged:Disconnect()
			connections[player].MaxHealthChanged = nil
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	for _, connection in connections[player] do
		connection:Disconnect()
	end

	connections[player] = nil
end)

I tried it and it sadly doesnt work

Make this script in ServerScriptService

I tried this and i got this error message: attempt to iterate over a nil value

1 Like

This should fix the problem:

local Players = game:GetService("Players")

local connections = {}

Players.PlayerAdded:Connect(function(player)
	connections[player] = {}

	connections[player].CharacterAdded = player.CharacterAdded:Connect(function(character)
		connections[player].MaxHealthChanged = player.leaderstats.MaxHealth.Changed:Connect(function(value)
			character.Humanoid.MaxHealth = value
		end)
	end)

	connections[player].CharacterRemoving = player.CharacterRemoving:Connect(function()
		if connections[player].MaxHealthChanged then
			connections[player].MaxHealthChanged:Disconnect()
			connections[player].MaxHealthChanged = nil
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	if connections[player] then
		for _, connection in connections[player] do
			connection:Disconnect()
		end

		connections[player] = nil
	end
end)

Thanks this worked!

30 lettersssssss

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.