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

MouseClick event doesn't fire?

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)

2 comments

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.

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.

1 answer

✓ Accepted answer

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!

1 comment

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!

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