How can I get the player character on the server side?

I have a server script inside this spike trap that I’m trying to only damage a monster and not the players. Yet I’m stumped on how to define player in a server script.


local part = script.Parent

part.Touched:connect(function(hit) 
	if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character  then
		local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
		humanoid1:TakeDamage(5)
		wait()



	end
end)
local part = script.Parent

part.Touched:connect(function(hit) 
	if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character  then
		local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		humanoid1:TakeDamage(5)
		task.wait()
	end
end)
1 Like

Yes but player is still not defined.

This defines you the player of the character that touched it

Yes but this isn’t defined:

hit.Parent ~= player.Character
local part = script.Parent

part.Touched:connect(function(hit) 
	if hit.Parent:FindFirstChild('Humanoid')   then
		local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		humanoid1:TakeDamage(5)
		task.wait()
	end
end)

It doesnt make sense, on one hand you want to check for that player, but on the second hand, you dont want.

No I want to see that hit.Parent isn’t a player’s character. So I don’t damage the player.

do this

local part = script.Parent

part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) and game.Players:GetPlayerFromCharacter(hit.Parent) == nil then
hit.Parent.Humanoid:TakeDamage(5)
end
end)

1 Like
local part = script.Parent

part.Touched:connect(function(hit) 
	if hit.Parent:FindFirstChild('Humanoid') and game.Players:FindFirstChild(hit.Parent) == nil   then
		local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
		humanoid1:TakeDamage(5)
		task.wait()
	end
end)
1 Like

thats not going to work, you need to change it so that the “game.Players:FindFirstChild(hit.Parent)” is “game.Players:FindFirstChild(hit.Parent.Name)”

it’s already been solved, but hey.

1 Like