Hello! I’m trying to make a lives system that sets the player back to full health and gives them a temporary force field and speed increase as long as they have lives remaining. However, I ran into a problem where the game sets the player health to max but then a few moments later sets it to zero and kills the player and I’m not sure why.
This is the local script that I put into StarterGui to test if it worked or not:
local Lives = 4
humanoid.HealthChanged:Connect(function(NewHealth)
if NewHealth <= 0 and Lives > 0 then
Lives -= 1
print("Losing a life!")
humanoid.Health = 1
humanoid.WalkSpeed = 20
local forceField = Instance.new("ForceField")
forceField.Visible = true
forceField.Parent = character
task.wait(1)
humanoid.Health = humanoid.MaxHealth
task.wait(3)
forceField:Destroy()
end
if Lives <= 0 then
print("No lives left!")
return
end
end)
first you are handling lives on the client (terrible idea) this will allow exploiters to just delete the script or manipulate it and now they have infinite lives
second setting the health on the client is werid, switch everything to the server
Alright, so how would I do that? I tried connecting the local script to a remote event to make the healing and life counting server sided, but all it does is print the outputs I put down, and does not give the player a forcefield or heal them. Did I fire the event wrong?
Here’s the new scripts for context:
Remote Event:
local LRE = game.StarterGui.LoseLife:FindFirstChild("LivesRemoteEvent")
local Lives = 4
LRE.OnServerEvent:Connect(function(Player)
local character = Player.Character
local humanoid = Player.Character:FindFirstChild("Humanoid")
if Lives > 0 and humanoid.Health <= 0 then
Lives -= 1
print("Losing a life!")
local forceField = Instance.new("ForceField")
forceField.Visible = true
forceField.Parent = character
humanoid.Health = humanoid.MaxHealth
print("Healed!")
forceField:Destroy()
end
if Lives <= 0 then
print("No lives left!")
return
end
end)
Revival localscript that calls the RE:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local Re = game.StarterGui.LoseLife:WaitForChild("LivesRemoteEvent")
humanoid.HealthChanged:Connect(function()
if humanoid.Health <= 0 then
Re:FireServer(humanoid)
end
end)
Sorry if it’s too much trouble, I’m still really new to using remote events.
Never let the client tell the server what is going on and how to proceed.
Reading when a player died or got damage should be placed in server and no need of client stuff.
When player joins, when character spawn/loaded, find the Humanoid, and connect a function to it. An event Humanoid.Died:Connect()
When that event triggers is when the player died, so, whenever that event triggers, call a function to check a server side table of how many lives the player still has, or what you want to achieve.
No need to use remotes for this, unless you want to show a GUI or something special on client screen, then you use the remote from server to client :FireClient(player, values), on a local script you receive that call, and handle the GUI you want to show.
No need at all to trust the client about when they got damage or died, or lives or anything