Still have questions? Join our Discord server and get real time help.
0

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)


1
RayCurse 1348
2 months ago

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.

0
can you give a script please? im not good at that stuff. Adenandpuppy 34 — 2mo
0
or at least give me the script on how if r is pressed down, it will run a script below it. Adenandpuppy 34 — 2mo
0
THIS deserves to be the accepted answer. and it is now. incapaxx 3107 — 2mo
0

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 :)

0
It did not work. i put the server code in the local script, and the client code in the Sever Script Service script. Adenandpuppy 34 — 2mo
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. Mariojumper5 26 — 2mo