Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Error showing up for remove event, how to fix?

Asked by 3 years ago

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

Log in to vote
0
Answered by 3 years ago

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)
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! Alisa_xd1 8 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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)                                                     
0
Hello! Thank you for your response however the issue was because I didn't defined the player variable correctly. Have a great day! Alisa_xd1 8 — 3y
Log in to vote
0
Answered by 3 years ago
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

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! Alisa_xd1 8 — 3y

Answer this question