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

How Do I Fix This onClicked Item Giver Script?

So I am making an item giver so when you click the part, it clones an item from ReplicatedStorage. Here is the code:

local player = game.Players.LocalPlayer

function onClicked()
	game.ReplicatedStorage.PaperSlip:clone().Parent = player.Backpack
end

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

It's in a localscript and it's right inside the part. I have a clickdetector right inside the part too, so I don't know what I did wrong, but it's not doing anything when I click it. Someone please help!

3 comments

0

Is your Script disabled?

0

No it isn't

0

Regular scripts can't access LocalPlayer. Also, LocalScripts don't work in workspace.

2 answers

✓ Accepted answer

First, LocalScripts do not work on the server. They only work on the client.

Now you're probably gonna ask, "How do I get the player who clicked without a local script?".

The answer is simple. Click Detectors have a player parameter.

What you need to do:

function Clicked(Player) -- Player param to get the player who clicked
	game.ReplicatedStorage.PaperSlip:clone().Parent = Player.Backpack
end

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

Simple!

I wrote this out in the browser and didn't test it.

3 comments

0

It works in Studio but not in the game...

0

This must be in a server script. You CANNOT use a local script.

0

Thank you! It worked!

local RP = game:GetService("ReplicatedStorage")
local Plr = game:GetService("Players").LocalPlayer
local BP = Plr:WaitForChild("Backpack")
local CD = script.Parent:WaitForChild("ClickDetector")

CD.MouseClick:connect(function()
	if RP:FindFirstChild("PaperSlip") then
		RP["PaperSlip"]:Clone().Parent = BP
	end
end)

Try this. Also make sure it is called: "PaperSlip". Not something like "Paperslip" or "paperslip".

1 comment

0

Sorry, it's not working...

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