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

How do I make a gun go in your backpack on click?

Asked by 6 years ago

game.Workspace.Gun.ClickDetector.MouseClick:connect(function() local plr = game.Players.LocalPlayer local Item = game.ReplicatedStorage:FindFirstChild("Gun") local p = Item:Clone() p.Parent = plr.Backpack end)

For some reason, this script won't work. The Gun is in ReplicatedStorage.

2 answers

Log in to vote
0
Answered by
Discern 1007 Moderation Voter
6 years ago

LocalPlayer does not work in a regular Script.

game.Workspace.Gun.ClickDetector.MouseClick:connect(function(plr) --Putting "plr" in the parameters for the MouseClick event will assign that name as a variable to the player who clicked it.
    local Item = game.ReplicatedStorage:FindFirstChild("Gun")
    local p = Item:Clone()
    p.Parent = plr.Backpack
end)

NOTE: Please format your code (shown above) so it is easier to read for people around ScriptingHelpers. This is done by pressing the Lua BBCode and pasting your code within the lines it makes when creating your post.

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Put this script inside of the Gun part.

script.Parent.ClickDetector.MouseClick:connect(function(player)
local rs = game:GetService("ReplicatedStorage")
rs.Gun:clone().Parent = player.Backpack
end)
0
ReplicatedStorage is intended as a replacement for the Lighting service. yougottols1 420 — 6y
0
Oh. Sorry. PyccknnXakep 1225 — 6y

Answer this question