Attempting to get humanoid with server script

Hello again.

So I have this locker system that you can hide in, it works fine, until you realize it doesn’t work in multiplayer properly.
Problem:
Well, it works in multiplayer for my test, but if a player happen to dies or leave while being in locker, my script doesn’t detect that, and it will still assume someone is using the locker, making it stuck.

This is the video of it.


(I held the button all the way at the end, although the text didn’t showed up because I made change to it)

What I want to do is to get humanoid from the server script, to detect if the player leave or died while being in the locker so the locker could unlock itself.
Though, I don’t know how to do that, I’ve tried to use findfirstchild, but that doesn’t work, or I don’t know how to use it properly.
I’ve tried to use local script and event, but afraid of it being exploited, so I removed it.
Here’s the workspace view:
image

script:

--Open tween
local tweenservice = game:GetService("TweenService")
local part = script.Parent.Parent.Hinge
local OriginalPos = script.Parent.Parent.Closed.Position
local TargetPos = script.Parent.Parent.Opened.Position
local OriginalOri = script.Parent.Parent.Closed.Orientation
local TargetOri = script.Parent.Parent.Opened.Orientation

local info = TweenInfo.new(
	0.3,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local Goal = 
	{
		Position = OriginalPos
		,
		Orientation = OriginalOri
	}



local tween = tweenservice:Create(part,info,Goal)

--Close tween
local tweenservice2 = game:GetService("TweenService")
local part2 = script.Parent.Parent.Hinge
local OriginalPos2 = script.Parent.Parent.Opened.Position
local TargetPos2 = script.Parent.Parent.Closed.Position
local OriginalOri2 = script.Parent.Parent.Opened.Orientation
local TargetOri2 = script.Parent.Parent.Closed.Orientation

local info2 = TweenInfo.new(
	0.3,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local Goal2 = 
	{
		Position = OriginalPos2
		,
		Orientation = OriginalOri2
	}



local tween2 = tweenservice2:Create(part2,info2,Goal2)

--The locker system

local Hiding = false
local Hider = nil
local HidingPos = script.Parent.Parent.HidePosition
local ExitPos = script.Parent.Parent.ExitPosition

function Hide(Clicker) --Thanks to RFL890 for helping on this part.
	if Hiding == false and Clicker.Character.Humanoid.Health >= 1 then
		Hiding = true
		Hider = Clicker.Name
		Clicker.Character.Humanoid.WalkSpeed = 0
		workspace.CurrentCamera.CameraSubject = Clicker.Character.Head
		Clicker.CameraMode = Enum.CameraMode.LockFirstPerson
		Clicker.Character.HumanoidRootPart.Anchored = true
		Clicker.Character.HumanoidRootPart.CFrame = CFrame.new(HidingPos.Position, ExitPos.Position)
		local Anim2 = Instance.new("Animation")
		Anim2.AnimationId = "rbxassetid://6534399775"
		Anim2.Name = "Hiding"
		Anim2.Parent = Clicker.Character
		local Hum = Clicker.Character.Humanoid:LoadAnimation(Clicker.Character.Hiding)
		Hum:Play()
	elseif Hiding == true then
		if Clicker.Character.HumanoidRootPart.Anchored == true then
			Hider = nil
			for _, k in ipairs(Clicker.Character.Humanoid:GetPlayingAnimationTracks()) do
				k:Stop()            
			end
			Clicker.Character.Humanoid.WalkSpeed = 11
			workspace.CurrentCamera.CameraSubject = Clicker.Character.Humanoid
			Clicker.Character.HumanoidRootPart.Anchored = false
			Clicker.Character.HumanoidRootPart.CFrame = CFrame.new(ExitPos.Position, HidingPos.Position)
			Clicker.CameraMode = Enum.CameraMode.Classic
			Hiding = false
		elseif Clicker.Character.HumanoidRootPart.Anchored == false then
			Clicker.PlayerGui.Subtitle.Text.Frame.Text = "This locker is already being used!"
			Clicker.PlayerGui.Subtitle.Text.Frame.TextTransparency = 0
			Clicker.PlayerGui.Subtitle.Text.Frame.TextStrokeTransparency = 0
			wait(3)
			Clicker.PlayerGui.Subtitle.Text.Frame.Frame.Text = "..."
			Clicker.PlayerGui.Subtitle.Text.Frame.TextTransparency = 1
			Clicker.PlayerGui.Subtitle.Text.Frame.TextStrokeTransparency = 1
		end
	end
end

script.Parent.ProximityPrompt.Triggered:Connect(Hide)

function Activating(Clicker)
	tween2:Play()
	script.Parent.LockerOpen:Play()
	Clicker.Character.Humanoid.WalkSpeed = 0
end

script.Parent.ProximityPrompt.PromptButtonHoldBegan:Connect(Activating)

function Deactivate(Clicker)
	tween:Play()
	script.Parent.LockerClose:Play()
	Clicker.Character.Humanoid.WalkSpeed = 11
end

script.Parent.ProximityPrompt.PromptButtonHoldEnded:Connect(Deactivate)

If you could help me with this it’d be appreciated.

2 Likes

You can use the function Humanoid.Died

1 Like

Yes but I couldn’t get to the humanoid because its a server script, I wanted to use local script but as I said I’m afraid of it being exploited.

you can use a local script in the same way

Ok make a variable that get the player and then use this:

local player = --how you want to get the player

local humanoid = player.Character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
    --Do what you need here
end)

Doesn’t work unfortunately.

Oh i didn’t mean it like that, you can try using a remoteevent, or what Ninja said

Ok but, I couldn’t get the player, I tried this

local player = game.Player.LocalPlayer
local hum = player.Character:WaitForChild("Humanoid")

hum.Died:Connect(function(Death)
	if hum.Parent.HumanoidRootPart.Anchored == true then
		Hiding = false
		end
end)

The locker is still stuck, I assume because LocalPlayer can’t be accessed from server script.

So the only way I’d access LocalPlayer with server script is to use a remote?

Did you do localplayer in a serverscript?

Yeah, although I knew it wouldn’t work, so I assume I’d have to use a remote?

if hum.Health == 0 then
      --deactivate
end

my way is not the best way but im showing you this bc you said other ways didnt work

No? if your tring to get the humanoid you would do

game.Players.PlayerAdded:Connect(function(player)
local character = player:WaitForChild("Character")
local humanoid = character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()

   end)
end)

As I saw in the video of a proximity prompt to get the player into the locker so try:

local ProximityPrompt = --Make the variable of the prompt

ProximityPrompt.Triggered:Connect(function(player)
    local humanoid = player.Character:WaitForChild("Humanoid")

    humanoid.Died:Connect(function()
        --Do what you need here
    end)
end)
1 Like

game:GetService("Players") get the Players object in the explorer so i cannot do it.

Just tried that, nothing came up in the output nor does it work, I’m sorry if I’m being confusing.

Forgot to mention I’m not going for humanoid specifically, I wanted to get to the player so I could change the camera type.
What I want is both the humanoid and to be able to change player camera once they died.

This actually worked! now my only problem is to be able to see if the player that’s hiding left the game rather than dying, I assume only remote can do this?

See @NinjaFurfante07’s response, it should help you.

Anyways, ProximityPrompt.Triggered comes with a player parameter. Just get the character and humanoid from there.

ProximityPrompt.Triggered:Connect(function(player)
   local humanoid = player.Character.Humanoid
end)
1 Like

Never knew that, I will try that right now.