local Players = game:GetService("Players")
script.Parent.Touched:Connect(function(hit)
local Player = Players:GetPlayerFromCharacter(hit.Parent)
game.ReplicatedStorage.TowerEntered:FireClient(Player)
end)
This is inside of a Server Script.
When I run that, it gives me the error “player argument must be a Player object” even though it is?
Not always, if hit.Parent is not a player character, :GetPlayerFromCharacter() will return nil.
You want to check if the method returned something by checking if Player is not nil.
local Players = game:GetService("Players")
script.Parent.Touched:Connect(function(hit)
local Player = Players:GetPlayerFromCharacter(hit.Parent)
if Player and Player:IsA("Player") then
game.ReplicatedStorage.TowerEntered:FireClient(Player)
end
end)
Use this function to check if the hit part is part of a character
function IsCharacter(part)
return part.Parent:FindFirstChild("Humanoid") ~= nil
end
Use of function
if IsCharacter(hit) then
-- get the player and fire the client here
end
Full thing
local Players = game:GetService("Players")
function IsCharacter(part)
return part.Parent:FindFirstChild("Humanoid") ~= nil
end
script.Parent.Touched:Connect(function(hit)
if IsCharacter(hit) then
local Player = Players:GetPlayerFromCharacter(hit.Parent)
game.ReplicatedStorage.TowerEntered:FireClient(Player)
end
end)