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

Why do these click to pick up scripts only work on studio and not in the actual game?

So I'm making a script where you can click to pick up things.. The problem is, it only works in studio and not in the actual roblox client.. Why does it do this? Help would be appreciated! Thanks! Here's the script:

function onClicked(player)
game.ServerStorage["PlayerHouseKey"]:Clone().Parent = game.Players.LocalPlayer.Backpack
	script.Parent:Remove()
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

1 comment

0

""The MouseClick event must be handled by a Script.""

1 answer

To start off, there's a couple of things you're doing wrong: if you're using a script to do this, you're trying to use 'LocalPlayer', which can only be used by LocalScripts. If you're using a LocalScript, you're trying to access ServerStorage, which isn't replicated to clients, and thus can't be accessed by LocalScripts. Also, while this isn't necesarrily a flaw, you should always use :WaitForChild() on things when using a LocalScript.

For example:

--Instead of...
script.Parent.ClickDetector.MouseClick:connect(function() end)
--Do this...
script.Parent:WaitForChild("ClickDetector").MouseClick:connect(function() end)

So, I'm assuming that you just have a part in Workspace with a script in it that, when clicked, will clone a tool into the backpack of the player who clicked it. To do that, we'll just use this little code:

local cD = Instance.new("ClickDetector", script.Parent)
local tool = game.ServerStorage['ExampleTool']
cD.MouseClick:connect(function(c)
 if not c.Backpack:FindFirstChild(tool.Name) then
  tool:Clone().Parent = c.Backpack
 end
end)

And that's that.

Hope this helps.

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