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

Trying to convert a leaderstats value to Health?

So I'm currently making a game where you can pick up orbs to gain Defense. Then, that defense is then converted via script to health, and 1 Defense = 1 Health. I'm currently using remote events but I still can't put my finger on it, any ideas? Currently, I'm stuck on converted defense to health via a script, because the defense is a client value, while health is a server value. I think remote events would work, but then I still don't know how the events would work. Help!

My Current 2 scripts: Local (fire client)

local defense = game.Players.LocalPlayer.leaderstats.Defense
local Event = game.ReplicatedStorage.HealthMultiplier

if defense.Value >= 0 then
	Event:FireServer()
end

I think I need to combined the two values together in one script, but thats not possible without remote events, then again after remote events I don't know how I would do it.

Script (recives client)

local defense = game.Players.LocalPlayer.leaderstats.Defense
local Event = game.ReplicatedStorage.HealthMultiplier
local Health = script.Parent.Humanoid.Health

Event.OnServerEvent:Connect(function()
	Health.Value = Health.Value + defense.Value*1
end)

I know this doesn't work because I'm using something local, but If I do a local script then I have to do something through a normal script, this confused work constantly repeats. I know its really confusing and you probably have no idea what I'm talking about. I've tried looking on youtube, discord servers, etc. But found nothing. Help would be appreciated!

1 answer

✓ Accepted answer

When firing local -> server, the first parameter is the player.

local script (starter player scripts, starter character scripts, or player gui)

local player = game.Players.LocalPlayer
local multiplier = 1
player:WaitForChild("leaderstats").Defense.Changed:Connect(function()
	game.ReplicatedStorage.HealthMultiplier:FireServer(player.leaderstats.Defense.Value, multiplier)
end

script (server script service or workspace)

game.ReplicatedStorage.HealthMultiplier.OnServerEvent:Connect(function(player, num,m)
	if player.Character then
		player.Character.Humanoid.Health = num * m
	end
end)

sorry if any typos. You do not need any loops or anything other than the scripts here. If this answered your question, please accept the answer. Otherwise, you can contact me on discord (in my bio) or respond here (preferred).

2 comments

0

No errors, however it does not convert the players defense stat to extra health.

0

Oh! if you want extra health, replace line three of the server script with: player.Character.Humanoid.Health = player.Character.Humanoid.Health + num * m

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