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

How do i make this script give the player health?

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)

1 comment

0

You would need to use a remote event to fill the humanoid’s health.

1 answer

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)

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