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

How To Reference A Backpack?

So I Have Been Trying To Reference A Backpack To Give The Player Chicken On A Click Giver I Looked On Roblox Wiki For How To And I Did That But It Didn't Work I Use This Script To Clone The Chicken All Over The Place So I Think It Is Just The Referencing I Don't Know I Am Still New To Lua And Coding This Is What I Did

click = game.Workspace.ChickenGiver.ClickDetector

click.mouseClick:Connect(function()
	game.ServerStorage.ChickenTool:Clone().Parent = game.Players.LocalPlayer.Backpack
end)

2 comments

0

You need a capital M for 'click.MouseClick'. The rest is fine.

0

Is this a server script? If so I you wouldn't be able to use Local Player, but you can use remote events to communicate with a local script & tell it to clone the tool & place it in the backpack.

1 answer

✓ Accepted answer

A ClickDetector will work on both the server and client side (they were updated to work with FE which is now default).

You should be running this as a server script as I am guessing you would like this to work for all players in the game and not just one player. One thing to not is that the event MouseClick will pass the player who clicked the detector which you can use.

The script itself is a simple fix as the majority of it is correct. You just need to access the player using the player object passed in the event. Also remember Lua is case sensitive so mouseClick is not the same as MouseClick

-- I prefer to use WaitForChild as it will throw a warning if not found within a set time. Strictly speeking you do not need to do this.
local tool = game:GetService('ServerStorage'):WaitForChild('ChickenTool'):Clone()

workspace.ChickenGiver.ClickDetector.MouseClick:Connect(function(plr)
	local backpack = plr:FindFirstChild('BackPack') -- prevent threads from blocking?
	if backpack then
		tool:Clone().Parent = backpack 
	end
end)

i hope this helps. Please comment if you have any other questions.

1 comment

0

Thank You So Much

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