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

How Do I Fix This onClicked Item Giver Script?

Asked by
itsJooJoo 195
8 years ago

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!

0
Is your Script disabled? manchester1002 57 — 8y
0
No it isn't itsJooJoo 195 — 8y
0
Regular scripts can't access LocalPlayer. Also, LocalScripts don't work in workspace. User#11440 120 — 8y

2 answers

Log in to vote
0
Answered by
AZDev 590 Moderation Voter
8 years ago

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.

0
It works in Studio but not in the game... itsJooJoo 195 — 8y
0
This must be in a server script. You CANNOT use a local script. AZDev 590 — 8y
0
Thank you! It worked! itsJooJoo 195 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
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".

0
Sorry, it's not working... itsJooJoo 195 — 8y

Answer this question