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

MouseClick event doesn't fire?

Asked by 6 years ago
Edited 6 years ago

I'm trying to dispense a player an item, but the following function doesn't work. Any ideas why? It would be nice to explain it.

local sandwich = script.Parent["Turkey Sandwich"]
local clicker = script.Parent.ClickDetector

function GiveSandwich(playerWhoClicked)
    local players = game:GetService("Players")
    local  human = players:FindFirstChild(playerWhoClicked.Name)
    if human then
        print("Successfully Found Player")
        local newsandwich = sandwich:Clone()
        newsandwich.Parent = human.Backpack
        print("Sandwich is dispensed and event completed")
    end
end

clicker.MouseClick:Connect(GiveSandwich)

0
You have not included any prints so you it looks like you have done no debugging. Your code also shows that you have not understood what this event passes as an argument. First include some debugging and find out what this script is doing. User#5423 17 — 6y
0
After I debugged it with Print, it has shown to debug once I add the name in Line 6. But it still doesn't dispense the Sandwich. Syntax_404 37 — 6y

1 answer

Log in to vote
0
Answered by
arshad145 392 Moderation Voter
6 years ago

Hello , First thing I noticed in your script was that you were trying to find the player's name from a service class game:GetService("Players") ... More information here ...

I replaced the whole concept of what you were trying to do , with tables

local sandwich = script.Parent["Turkey Sandwich"]
local clicker = script.Parent.Clickdetector
local human 
function GiveSandwich(playerWhoClicked)
    local players = game:GetService("Players")
    for i , v in pairs (players:GetChildren()) do
        if v.Name == playerWhoClicked.Name then -- can be replaced with characters and also player object itself...
            human = v 
        end
    end
    if human then
        print("Successfully Found Player")
        local newsandwich = sandwich:Clone()
        newsandwich.Parent = human.Backpack
        print("Sandwich is dispensed and event completed")
    end
end

clicker.MouseClick:Connect(GiveSandwich)


Thank you for reading! Wish you goodluck for future scripts!

0
Thanks for the explanation! I modified your work and deleted Line 3, as well as making "ClickDetector" in line 2 case sensitive, then it worked. Once again, thanks! Syntax_404 37 — 6y
Ad

Answer this question