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

How do i make this script give the player health?

Asked by 14 days ago

So basically, this script is in StarterPack, and it makes it so when you press the R key down, you restore your full health by making the humanoid have that much health. the script is not working after i accidentally did something to it, and to change the version history to when it was working would be FATAL to the building portion of my game. any help please?

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)

if key == "R" then

Parent.Humanoid.Health = 100

end

end)

0
You would need to use a remote event to fill the humanoid’s health. Rheines 552 — 14d

1 answer

Log in to vote
0
Answered by 13 days ago

Option 1

Well if you are going to do this on the client, use the UserInputService rather than the Mouse's KeyDown, as well as changing the deprecated connect to Connect

Revised Local Script

local player = game:GetService("Players").LocalPlayer

game:GetService("UserInputService").InputBegan:Connect(function(input, event)
    if input.KeyCode == Enum.KeyCode.R then
        player.Character:WaitForChild("Humanoid").Health = 100
    end
end)

Option 2

However, I would suggest doing this on the Server since then the Health will change for the player for all others to see, and not just the client the local script is being used for:

Server Script under ServerScriptService

local remote = Instance.new("RemoteEvent")
remote.Name = "HealthReload"
remote.Parent = game:GetService("ReplicatedStorage")

remote.OnServerEvent:Connect(function(player)
    player.Character:WaitForChild("Humanoid").Health = 100
end)

Local Script under StarterGui

local remote = game:GetService("ReplicatedStorage"):WaitForChild("HealthReload")

game:GetService("UserInputService").InputBegan:Connect(function(input, event)
    if input.KeyCode == Enum.KeyCode.R then
        remote:FireServer()
    end
end)
Ad

Answer this question