How to use :clone with a click detector

Probably a really dumb question, I don’t use :clone often how would I clone a gear from ServerStorage into the players backpack using a click detector?

my current code:

game.Workspace.CollectLampEvent.OnServerEvent:Connect(function()
game.ServerStorage.Lamp:Clone(game.Players.LocalPlayer.Backpack)
end)
2 Likes
--// Services
local ServerStorage = game:GetService("ServerStorage")

--// Constants
local Part = game.Workspace.Part
local ClickDetector = Part.ClickDetector
local Lamp = ServerStorage:FindFirstChild("Lamp")

--// Event handler
Part.ClickDetector.MouseClick:Connect(function(player)
       Lamp:Clone().Parent = player.Backpack
end)

If you need an explanation, just ask!

1 Like

Ok, will check the script and if I have issues I will say!

Thanks for contribution!

1 Like

Awesome! If it works be sure to mark the solution so readers know it’s been fixed :stuck_out_tongue:

Clicked it, nothing. where is the script supposed to be?

Serverscriptservice or workspace. Make sure that the constants are correct!

1 Like

Oh! I forgot to edit the constants haha. Thanks for reminding me!

1 Like

Could I put a remote event in serverscriptservice?

1 Like

Why do you need a remote event? But no, you cannot. The client can’t access that. Put it in replicated storage in the event you need to fire one.

Any constants I haven’t filled out?

--// Services
local ServerStorage = game:GetService("ServerStorage")

--// Constants
local Part = game.Workspace.HOLDLANTERN1
local ClickDetector = Part.LampClick
local Lamp = ServerStorage:FindFirstChild("Lamp")

--// Event handler
Part.ClickDetector.MouseClick:Connect(function(player)
Lamp:Clone().Parent = player.Backpack
end)
1 Like

I made a mistake in my code. Your constants are good, but I messed up the event handler.

--// Services
local ServerStorage = game:GetService("ServerStorage")

--// Constants
local Part = game.Workspace.HOLDLANTERN1
local ClickDetector = Part.LampClick
local Lamp = ServerStorage:FindFirstChild("Lamp")

--// Event handler
ClickDetector.MouseClick:Connect(function(player)
       Lamp:Clone().Parent = player.Backpack
end)

It was referencing the literal ClickDetector, not the variable.

1 Like

Ok! It works!

I’ve finally made a few more advancements for my game!

Thank you!

1 Like

Congrats! Good luck creating!!!

I have been holding off scripting because the builds and puzzles my brother built for my game were overwhelming and I had exam revision but I felt a bit productive today and decided to do some advancements.

Take advantage of those days! Work hard and you’ll find success.

1 Like