I’m trying to make it so that touching a part in the server will connect the player to a remote function, starting the game. However, it gives me this error:
FireClient: player argument must be a Player object
I made sure I defined the variable using GetPlayerFromCharacter but it still doesn’t work. Here’s the code:
local debounce = true
script.Parent.Touched:Connect(function(plr)
local playerService = game:GetService("Players")
local player = playerService:GetPlayerFromCharacter(plr.Parent)
if debounce then
local modelPlayer = plr:FindFirstAncestorWhichIsA("Model")
debounce = not debounce
if modelPlayer then
plr.Parent.PrimaryPart.Position = workspace.Telepart.Position
end
else
warn("TouchPart is accessory")
debounce = true
end
game.ReplicatedStorage["Start Game"]:FireClient(player)
task.wait(1)
debounce = true
end)
If you noticed on line 4, I already defined the player. This should not be happening. Can you help me? Thanks!
-NeoGaming_RBLX
you just wrote plr you did not check if it is a player or just a object if that part is on the baseplate it will touch baseplate
You assume that GetPlayerFromCharacter will return a player object. However, if it another random part hits it, it will return nil, hence the error.
we wrote the same thing nice (30 letters)
Following your replies would this work:
local player
local debounce = true
script.Parent.Touched:Connect(function(plr)
local playerService = game:GetService("Players")
if debounce then
local modelPlayer = plr:FindFirstAncestorWhichIsA("Model")
debounce = not debounce
if modelPlayer then
plr.Parent.PrimaryPart.Position = workspace.Telepart.Position
player = playerService:GetPlayerFromCharacter(plr.Parent)
end
else
warn("TouchPart is accessory")
debounce = true
end
game.ReplicatedStorage["Start Game"]:FireClient(player)
task.wait(1)
debounce = true
end)
yes it would work but you could just write if plr:FindFirstChild(“Humanoid”) then (thenyour script)
Rewrote it because I like cleaning code up.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local telepart = workspace.Telepart
local debounce = false
script.Parent.Touched:Connect(function(hit)
if debounce then return end
debounce = true
local character = hit:FindFirstAncestorWhichIsA("Model")
local player = character and Players:GetPlayerFromCharacter(character)
if player then
character.PrimaryPart:PivotTo(telepart.CFrame)
ReplicatedStorage["Start Game"]:FireClient(player)
else
warn("No player found")
end
task.wait(0.5)
debounce = false
end)
Thanks for the help lol 0 errors in output thanks to you guys