Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by 2 months ago

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

Log in to vote
1
Answered by
RayCurse 1348 Moderation Voter
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
Ad
Log in to vote
0
Answered by 2 months ago

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

Answer this question