Referencing to a Player's Humanoid Help

The goal here is to kill the player once they trigger the proximity prompt, how do I reference the humanoid here?

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
local part = script.Parent


part.ProximityPrompt.Triggered:Connect(function(character)
	humanoid.Health = 0
	character.Humanoid.Health = 0
end)
2 Likes

U should do like this:

character = Player.Character or player.CharacterAdded:Wait()
humanoid = character:WaitForChild("Humanoid")

EDIT: I made a huge mistake! I told you to put the Local Script in Workspace! This will not work! For now just put it in StarterGui

I assume you are using a “regular” script, in that case: “Regular” scripts cannot access the local player, only Local Scripts can. You have to use a RemoteEvent, fire it in the script when the prompt is triggered, then, on a local script in StarterGui kill the player when the event is fired. In code this would look like this:

The script (The one in the proximity prompt)

local part = script.Parent

part.ProximityPrompt.Triggered:Connect(function(player)
   game:GetService("ReplicatedStorage").KillEvent:FireClient(player) --Put a RemoteEvent in Replicated Storage called "KillEvent"
end)

The Local Script (In StarterGui)

local LocalPlayer = game:GetService("Players").LocalPlayer
local Character = LocalPlayer.Character
local Humanoid = Character:WaitForChild("Humanoid")

game:GetService("ReplicatedStorage").KillEvent.OnClientEvent:Connect(function()
        Humanoid.Health = 0
end)

Ill try it but I find it strange it would be in StarterGui

You should be setting the humanoid health on the server, not on the client. This can be done by firing an event on the client, receiving it on the server and setting the health to 0 on the server.

This was the solution, but why did it have to be in startergui?

Because it has to be on the client side, such as StarterGui or StarterPlayerScripts, but StarterPlayerScripts can’t seem to access RemoteEvents.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.