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

Local script dosent save max health upgrades?

So i made a script that is supposed to upgrade your max health but i tried making it so it saves even after your death But the script didnt work Here is the script:

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local respawnTime = 3

player.CharacterAdded:Connect(function(char)
	local humanoid = char:WaitForChild("Humanoid")
	if humanoid then
		humanoid.Died:Connect(function()
			wait(respawnTime)
			player.Character.Humanoid.MaxHealth = leaderstats.MaxHealth.Value
			player.Character.Humanoid.Health = leaderstats.MaxHealth.Value
		end)
		end
end)

2 comments

0

Also just a tip: you don't need to check if 'humanoid' exists if you're using WaitForChild.

0

Im trying to get the maxhealth from the leaderstats doe

1 answer

Well, a few things here.

1. You shouldn't be changing humanoid properties that should be replicated to other players in a localscript. This should be done server-side (in a server script), or through a localscript that fires a server-side event (using remote events).

2. I don't believe you need to use a Died event on the humanoid in order to re-assign the max health. You should just be able to wait for the character to respawn, then change the max health.

Here is an implementation of this inside of a server script:

local Players = game:GetService("Players");

-- some default max health you want to assign
local DEFAULT_MAX_HEALTH = 500;

local onPlayerAdded = function(newPlayer)
	newPlayer.CharacterAdded:Connect(function(char)
		local humanoid = char:WaitForChild("Humanoid")
	
		-- update the humanoid's health/max health
		humanoid.MaxHealth = DEFAULT_MAX_HEALTH;
		humanoid.Health = humanoid.MaxHealth;
	end))
end

Players.PlayerAdded:Connect(onPlayerAdded);

Let me know if you have any questions

1 comment

0

Where do i put script

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