What do you want to achieve? Keep it simple and clear!
a: a fix to my issue
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.
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
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)
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)