Checking what player is clicking

with FilteringEnabled I’d like to check what the player has clicked.

So for example they click Eggs and they’re given a tool named Eggs… this is what I attempted but unfortunately it didn’t work -

script.parent.ClickDetector.MouseClick:connect(function(player)
local t = Instance.new(“Tool”)
t.Name = “Eggs”
t.Parent = player[“Backpack”]
end)

1 Like

This should be:

script.parent.ClickDetector.MouseClick:connect(function(player)
local t = Instance.new(“Tool”)
t.Name = “Eggs”
t.Parent = player:FindFirstChild("Backpack")
end)

Can you specify the need for the FilteringEnabled? I’m kinda lost as to what you need.

1 Like

Is this from a local script or a server script. I don’t believe filtering enabled would affect this. If you mean that the server cannot see this tool, you could just use a remote event to tell the server to give them a tool.

Oh, thank you! It was just something I thought I’d need to specify.

I’m curious though, does using player[“Backpack”] not act as FindFirstChild anymore? I used to use [“”] all the time when referring to the child of something.

I believe that player[“Backpack”] is just like saying Player.Backpack.

I wouldn’t know the answer to that. Whenever I try to find something, I just use FindFirstChild or WaitForChild depending on what I need.

Since you said filtering enabled, is this script a local script? If so, local scripts will not run in the workspace.

It’s a ServerScript I believe I was just a little confused, thanks for checking though!

Now when the player clicks “Eggs” they’ve given a tool called Eggs but I’d like to make it so when they click “Bowl” the tool “Eggs” is removed from Backpack… This was my attempt-

clickDetector = script.Parent[“ClickDetector”]
function onMouseClick(player)
local pb = player:FindFirstChild(“Backpack”)
local item = pb:FindFirstChild(“Eggs”)
item:remove()
end

Remove is depreciated, destroy is much better.

clickDetector = script.Parent[“ClickDetector”]
function onMouseClick(player)
    print("Clicked")
    local pb = player:FindFirstChild(“Backpack”)
    local item = pb:FindFirstChild(“Eggs”)
    if item then
        item:Destroy()
    end
end
clickDetector.MouseClick:Connect(onMouseClick)
1 Like

Perfect! Thanks for all your help guys.

1 Like