Remote Event Help || Error FireClient: player argument must be a player object

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am making a horror game where the player is jump scared when the hitbox is touched

  2. What is the issue? Include screenshots / videos if possible!
    12:21:46.793 FireClient: player argument must be a Player object - Studio - Script:22
    I don’t know how to get the player argument into the remoteEvent

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to create a variable for the local player outside the function, which does not work.
    I have searched on the DevForum, but I couldn’t personally find anything on it.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Script:

-- 
local animation = script:WaitForChild("Animation")
local humanoid = script.Parent:WaitForChild("Humanoid")
local play = humanoid:LoadAnimation(animation)
local jumpscareEvent = game.ReplicatedStorage:WaitForChild("JumpScare")
local person = game.Players.LocalPlayer

local hitbox = game.Workspace.Assets.hitbox
local debounce = false

play.Looped = false

local function box(player)
	hitbox.Touched:Connect(function(player)
		if not debounce then
			local h = player.Parent:WaitForChild("Humanoid")
			wait(0.3)
			h.WalkSpeed = 0	
			debounce = true
			play:Play()
			wait(1.5)
			script.Parent:Destroy()
			jumpscareEvent:FireClient(player)
		end
	end)
end

hitbox.Touched:Connect(box)

Local Script:

-- 
local Jumpscare = game.ReplicatedStorage:WaitForChild("JumpScare")
local JumpScareFrame = game.StarterGui.JumpScare.Frame

JumpScareFrame.Visible = false


local function scare(player)
	if player then
		JumpScareFrame.Visible = true
	end
end

Jumpscare.OnClientEvent:Connect(scare)

I am new to coding and the dev forum, so I don’t completely understand how everything works.
Sorry
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

The error is telling you that your player variable isn’t a valid Player instance. This seems to be happening because you’re assuming the parameter from the Touched event is a player when it’s actually the Part that has touched the hitbox Part.

You cant get the player when calling the Touched event. You need to get the player from the player’s character.
Your server script should be looking like this:

local function box(hit)
	if not hit.Parent:FindFirstChildWhichIsA("Humanoid") then
		return
	end
	
	if not debounce then
		local h = hit.Parent:WaitForChild("Humanoid")
		local player = game.Players:GetPlayersFromCharacter(hit.Parent)
		wait(0.3)
		h.WalkSpeed = 0	
		debounce = true
		play:Play()
		wait(1.5)
		script.Parent:Destroy()
		if player then
			jumpscareEvent:FireClient(player)
		end
	end
end

hitbox.Touched:Connect(box)

There are some other major issues in your script. First of all: you can’t get the LocalPlayer in a server-side script, so the person variable is nil. Second of all, why do you have a Touched event inside another Touched event that checks for the same exact collision?

I’m not sure what I was exactly thinking, but I thought that if I wanted to get the character I would have to use the touched event to create parameters that would set the walkspeed of the person who stepped on it to 0.

When I put the script in, it causes an error that says:

WaitForChildWhichIsA is not a valid member of Model “Workspace.xXAgentX”

i edited the code above, copy the code and paste it in your script again

The script is making another error stating:

12:53:18.022 GetPlayersFromCharacter is not a valid member of Players “Players” - Server - Script:19
The Line is:
local player = game.Players:GetPlayersFromCharacter(hit.Parent)

1 Like

Oops my bad.
Here is your proper code:

local function box(hit)
	if not hit.Parent:FindFirstChildWhichIsA("Humanoid") then
		return
	end
	
	if not debounce then
		local h = hit.Parent:WaitForChild("Humanoid")
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		wait(0.3)
		h.WalkSpeed = 0	
		debounce = true
		play:Play()

		wait(1.5)

		if player then
			jumpscareEvent:FireClient(player)
		end

		script.Parent:Destroy()
	end
end

hitbox.Touched:Connect(box)
1 Like

Sorry for bothering you, but it’s creating another log saying:
13:06:49.287 FireClient can only be called from the server - Client - LocalScript:141
Have I put the code into the wrong script?

This is the code for the local script

local Jumpscare = game.ReplicatedStorage:WaitForChild("JumpScare")
local JumpScareFrame = game.StarterGui.JumpScare.Frame

JumpScareFrame.Visible = false


local function scare(player)
	if player then
		JumpScareFrame.Visible = true
	end
end

Jumpscare.OnClientEvent:Connect(scare)

The code that I gave you should be in the server script, not a local script. Where did you place the code?

I put the code into the server script, I’m not sure why it’s causing the error

local animation = script:WaitForChild("Animation")
local humanoid = script.Parent:WaitForChild("Humanoid")
local play = humanoid:LoadAnimation(animation)
local jumpscareEvent = game.ReplicatedStorage:WaitForChild("JumpScare")


local hitbox = game.Workspace.Assets.hitbox
local debounce = false

play.Looped = false

local function box(hit)
	if not hit.Parent:FindFirstChildWhichIsA("Humanoid") then
		return
	end

	if not debounce then
		local h = hit.Parent:WaitForChild("Humanoid")
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		wait(0.3)
		h.WalkSpeed = 0	
		debounce = true
		play:Play()
		wait(1.5)
		script.Parent:Destroy()
		if player then
			jumpscareEvent:FireClient(player)
		end
	end
end

hitbox.Touched:Connect(box)

Strange… The error is saying that you are using FireClient() on a local script. FireClient() can only be used on a server script.
Are you sure the code you have given right above this post is in a server script or a local script?

well i think the issue is that on your first Script your firing a event to the player except your directing the event to whatever the object that touched the hitbox is. its a simple and easy fix, simply check if what touched the box has a humanoid to know if its a character or not and then get the character and find the player from that. Your script should look something like this:

local animation = script:WaitForChild("Animation")
local humanoid = script.Parent:WaitForChild("Humanoid")
local play = humanoid:LoadAnimation(animation)
local jumpscareEvent = game.ReplicatedStorage:WaitForChild("JumpScare")
local person = game.Players.LocalPlayer

local hitbox = game.Workspace.Assets.hitbox
local debounce = false

play.Looped = false

local function box(Hit) --since the variable you had here wasnt actually the player i changed it to Hit as in whatever touched the box.

	-- hitbox.Touched:Connect(function(player) -- this isnt needed since your just repeating the previous function so get rid of this

	if not debounce then
		if Hit.Parent:FindFirstChild("Humanoid") then --check if theres a humanoid

			local h = Hit.Parent:WaitForChild("Humanoid") --no need to use WaitForChild since it should be already loaded but it wont affect anything

			local Char = Hit.Parent --get the Character
			local Player = game.Players:GetPlayerFromCharacter(Char) --and now you have the player
			wait(0.3)
			h.WalkSpeed = 0	
			debounce = true
			play:Play()
			wait(1.5)
			jumpscareEvent:FireClient(Player) --now you can fire the Event with the actual Player
			script.Parent:Destroy() --this should be the last code that should be played, if this is played before another code it would destroy it self and the code after this wouldnt play

		end
	end
end

hitbox.Touched:Connect(box)
1 Like

Whoopsie, That was another script causing that error. The code does in fact work, the reason why I thought it didn’t work at first was because the variable for the screengui was the for the startergui instead of the playergui. Thank you!

2 Likes