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

How do you get Humanoid on a Server Script?

Hello! I want to know how to get Humanoid on a Server Script.

I have an alive IntValue on the Values folder located in Workspace.

I am making a round system game. Whenever a player enters the round, the alive value increases by +1.

When the alive value = 0, the round ends.

I managed to do that, but now I'm stuck with detecting the humanoid.

I do not want to use any loops or playeradded codes because it wouldn't work.

Can somebody help me? Here's the code:

local function SubtractAliveCount(player)
	local Alive = game.Workspace.Values.Alive
	Alive.Value = Alive.Value - 1
end
		
Humanoid.OnDied:Connect(SubtractAliveCount)

Thanks!

1 comment

0

pov: u believe that the server is a player

2 answers

This can be solved by accessing the humanoid from the client, then using RemoteEvents to pass the information to the Server, instead of doing game.Players.PlayerAdded or Loops. That method is actually a unique way to grab the humanoid on the server. This was not tested by me, just said through pure knowledge. Let's show an example of this taking action:

Client script:

local player = game.Player.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
wait(0.5)
RemoteEvent:FireServer(player)

Then connect to it in the server and do whatever. But in your case, it probably won't work. But I hope this helps you understand another way to do it besides your typical game.Players.PlayerAdded or Loop methods. Also, this is how to connect it to the server: Server example of connecting:

local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")

RemoteEvent.OnServerEvent:Connect(function(player)

end)

Never mind, by default it already passes the player value to tell which player fired the event. Ignore this part. Well, this took a long time to make. Bye, and I hope this helps! :D

6 comments

0

Thank you for your effort, but the 1st block of script is only for LocalScript, since you can't access player by doing game.Players.LocalPlayer. But, I appreciate it!

0

you can most DEFINITELY access the player by using game.Players.LocalPlayer in a local script.

0

Yea, sorry for the game.Player, a typo

0

Also the one below the client script is meant for a LocalScript, you know that right? Just like @Opalmime said

0

I mean the text that says "Client script

0

Also what you want is easy. Get every player by :GetPlayers. Check if each and every player has a model in the workspace with the same name. Then check if they are in the map range like putting a invisble and no colliding box and checking if the player is currently touching it. Thne for every player that is touching, increase the value + 1. When the round timer ends, just set it to zero. Easy!

local function PlayerAdded(player)
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
			print(player.Name .. "died!!!")
		end)
	end)
end

game:GetService("Players").PlayerAdded:Connect(PlayerAdded)

-- This extra code will make sure PlayerAdded function works

for _, player in pairs(game:GetService("Players"):GetPlayers()) do
	PlayerAdded(player)
end

1 comment

0

Self explanatory, but the print will be fired once a player died in the game, so hope you learn something new

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