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

Error showing up for remove event, how to fix?

Hello!

I want to fire an event if the player used a key card upon being touched. The "real" variable searches if it is a key card or not. Everything works fine except for the fire event which prints an error "FireClient: player argument must be a Player object" I tried for 30 minutes to fix this up but no idea came, if anyone can help me? Thank you!

script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local real = hit:FindFirstChild("Real")
	if real then
		game.ReplicatedStorage.KeyCardDestroy:FireClient(player) -- Error
	end
end)

3 answers

✓ Accepted answer

ok, here we go again.. so im taking it the keycard has the "Real" value inside of it and that the keycard is a tool so

script.Parent.Touched:Connect(function(hit)
	local real = hit.Parent:FindFirstChild("Real")--
	if real then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent.Parent)--you get the player from the character which is the tool parent, the handle of the tool being the child of tool so you need 2 .Parent to actually get to the player
		game.ReplicatedStorage.KeyCardDestroy:FireClient(player)
	end
end)

1 comment

0

Thank you, thank you!!! It was because I needed two parents since the tool was made of two parts, thank you so much! Have a nice day!

This script might work, here is the script.
Script:

script.Parent.Touched:Connect(function(hit)
       local player = game.Players:FindFirstChild(hit.Parent)
       local real = hit.Parent:FindFirstChild("Real")
       if real then
             game.ReplicatedStorage.KeyCardDestroy:FireClient(player) 
       end
end)                                                                                  		  	 

If real's parent is in player then, use this script. It might work.
Script:

 script.Parent.Touched:Connect(function(hit)
         local player = game.Players:FindFirstChild(hit.Parent)
         local real = player:FindFirstChild("Real")
         if real then
               game.ReplicatedStorage.KeyCardDestroy:FireClient(player) 
         end
  end)                                                     

1 comment

0

Hello! Thank you for your response however the issue was because I didn't defined the player variable correctly. Have a great day!

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then --checks if player is found because sometimes the part touches the ground or something and returns nil instead of player
		if hit.Parent:IsAncestorOf("Real") then --checks if the character has a keycard equipped, I went with the IsAncestorOf function instead since I think it would be more consistent
			game.ReplicatedStorage.KeyCardDestroy:FireClient(player)
		end
	end
end)

Let me know if there are any problems, I haven't tested this

1 comment

0

Hello, thank you for the response however the problem was that I had to set two parents(hit.Parent.Parent). Have a nice day!

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