Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I use function(hit) as a different hit function?

Hi, so, I'm making a game where if you touch a part, a GUI becomes visible. I get an error that says FireClient: player argument must be a Player object Could I possibly get some help? Any answer that works will do. It's a workspace script inside of the part. Also, if you were wondering, I have the GUI LocalScript in the target GUI, so that one isn't a problem. Here's the script that I have:

local remEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent3")

local debounce = false

script.Parent.Touched:Connect(function(player)
	
	if not debounce then
	
	debounce = true
	
	remEvent:FireClient(player)
	
	wait(999.999)
	
	debounce = false
		
		end
	
end)

1 comment

0

Hit refers to the Physical Instance that made contact with the respective BasePart. This would be the body part of a Player's Character Rig if it were to touch something. You need to reference the Model through Hit.Parent (as the body part is a Child of the Model), and then pass it through the 'GetPlayerFromCharacter' method of the 'Players' Service.

2 answers

✓ Accepted answer

the hit isnt the player, its the object that touched it; use this

local remEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent3")

local debounce = false

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:getPlayerFromCharacter(hit.Parent)
	if player then
		if not debounce then
			debounce=true
			remEvent:fireClient(player)
		end
	end
end)

So the problem is that you can't get the player through a touched event. Because when you write player, the game automatically thinks you're referring to the part that got touched. So what you woul need to do is this

	local remEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent3")
	 
	local debounce = false
	 
	script.Parent.Touched:Connect(function(hit)
	    local Humanoid = hit.Parent:FindFirstChild("Humanoid") 
                     if Humanoid then
                     local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
         
            
	    if not debounce then
	 
	    debounce = true
	 
	    remEvent:FireClient(Player)
	 
	    wait(999.999)
	 
	    debounce = false
	 
	             end
	       end
	end)

This discussion is preserved as it was. New answers, comments, and votes are closed.