Hi,
I created Remote Event that teleports player and play music, here’s error:
FireClient: player argument must be a Player object - Server - Script:8
Here’s script:
local Replicated = game:GetService("ReplicatedStorage")
local Remote = Replicated:WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer
function onTouched(m)
p = m.Parent:FindFirstChild("Humanoid")
if p ~= nil then
Remote:FireClient(player) -- error
end
end
script.Parent.Touched:Connect(onTouched)
2 Likes
This is a server script, LocalPlayer
is always nil
when trying to get it from a server script. You need to get the player yourself, which is simple, get the parent of what hit the part and use game:GetService("Players"):GetPlayerFromCharacter()
on it, so
local player = game:GetService("Players"):GetPlayerFromCharacter(m.Parent)
Inside of the touched event, check if it had found a player since an npc can also trigger your Humanoid check and use that as the argument for fireclient
Make sure to get rid of your LocalPlayer variable as it could cause the same issue to happen again
4 Likes
Thanks, it works but I don’t know why it teleports me 4-6 times at the same time (and music is glitching at start) h0w can i fix it?
Here is my output when I teleport:
09:03:48.989 FireClient: player argument must be a Player object - Server - Script:6
09:03:48.990 Stack Begin - Studio
09:03:48.990 Script 'Workspace.HJ.TouchPart.Script', Line 6 - 'function' onTouched - Studio - Script:6
09:03:48.990 Stack End - Studio
09:03:48.991 FireClient: player argument must be a Player object - Server - Script:6
09:03:48.992 Stack Begin - Studio
09:03:48.992 Script 'Workspace.HJ.TouchPart.Script', Line 6 - 'function' onTouched - Studio - Script:6
09:03:48.993 Stack End - Studio
09:03:48.993 FireClient: player argument must be a Player object - Server - Script:6
1 Like
It’s cause you don’t have a debounce for the Touched event, so it will happen multiple times, have a boolean variable set up so that when it finds a player and the variable is false
, it sets the variable to true
, fires to the client, waits a second or more and then sets the variable to false
so it can be used again
1 Like
Is it okay?
local Replicated = game:GetService("ReplicatedStorage")
local Remote = Replicated:WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer
local debounce = false
function onTouched(m)
if not debounce then
debounce = true
Remote:FireClient(game:GetService("Players"):GetPlayerFromCharacter(m.Parent))
debounce = false
else
return
end
end
script.Parent.Touched:Connect(onTouched)
1 Like
2 things
No there needs to be a wait time inbetween the debounce because it’ll still fire multiple times, and no need for that else return
in this case
You’re not checking if the return of GetPlayerFromCharacter
is nil
, hence why you still get taht error
1 Like
I changed to this:
local Replicated = game:GetService("ReplicatedStorage")
local Remote = Replicated:WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer
local debounce = false
function onTouched(m)
if not debounce then
if game:GetService("Players"):GetPlayerFromCharacter(m.Parent) then
debounce = true
Remote:FireClient(game:GetService("Players"):GetPlayerFromCharacter(m.Parent))
wait(1)
debounce = false
end
end
end
script.Parent.Touched:Connect(onTouched)
I’d recommend this
local Replicated = game:GetService("ReplicatedStorage")
local Remote = Replicated:WaitForChild("RemoteEvent")
local debounce = false
function onTouched(m)
local player = game:GetService("Players"):GetPlayerFromCharacter(m.Parent)
if debounce or not player then
return
end
debounce = true
Remote:FireClient(player)
wait(1)
debounce = false
end
script.Parent.Touched:Connect(onTouched)
Simpler to look at