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

This script gives the player health, but immedtailty taes it away, can someone please help?

So this is a local script that is inside Starter Player, that has a Remote Event. when this script is ran when pressing R, it adds the health you need (Yay! :D) then takes away the health you gained seconds later, to return to health back to its original percent. (No! D:) If anyone could help with this, that would be great.

local repStorage = game:GetService("ReplicatedStorage")

local remote = repStorage:WaitForChild("HealEvent")

  

remote:FireServer("argument")

  

game:GetService("UserInputService").InputBegan:Connect(function(key)

if key.KeyCode == Enum.KeyCode.R then

game.Players.LocalPlayer.Character.Humanoid.Health = game.Players.LocalPlayer.Character.Humanoid.Health + 99

end

end)

2 answers

✓ Accepted answer

This is in a local script which means that changes you make to the properties of the humanoid are not replicated to the server. The reason you have run into this problem is because the client changes the humanoid health while the server tells the client that the actual health is something different. The server's instance of the health takes higher precedence so the health is immediately changed back.

If you want to fix this, you will have to change the health on the server. Create another remote event in replicated storage and fire it whenever the key R is pressed.

3 comments

0

can you give a script please? im not good at that stuff.

0

or at least give me the script on how if r is pressed down, it will run a script below it.

0

THIS deserves to be the accepted answer. and it is now.

The reason why it changes the health back is because you are doing it on the client, not the server. Here is the code to do this:

Client Code:

  local repStorage = game:GetService("ReplicatedStorage")
    
    local remote = repStorage:WaitForChild("HealEvent")
    
    local remote2 = repStorage:WaitForChild("HealEvent2")
      
    
    remote:FireServer("argument")
    
      
    
    game:GetService("UserInputService").InputBegan:Connect(function(key)
    
    if key.KeyCode == Enum.KeyCode.R then
    remote2:FireServer()
    end
    
    end)

Server Code:

HealthTakesAway = 99
game.RelicatedStorage.HealEvent2.OnServerEvent:Connect(function(plr)
if workspace:FindFirstChild(plr.Name) then
workspace[plr.Name].Humanoid.Health = workspace[plr.Name].Humanoid.Health + HealthTakesAway
end
end)

Hope this helped you :)

2 comments

0

It did not work. i put the server code in the local script, and the client code in the Sever Script Service script.

0

Well put the server code in SERVER script service and the CLIENT code in a local script. A local script only executes code on the client's side.

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