So I am making an item giver so when you click the part, it clones an item from ReplicatedStorage. Here is the code:
1 | local player = game.Players.LocalPlayer |
2 |
3 | function onClicked() |
4 | game.ReplicatedStorage.PaperSlip:clone().Parent = player.Backpack |
5 | end |
6 |
7 | 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!
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:
1 | function Clicked(Player) -- Player param to get the player who clicked |
2 | game.ReplicatedStorage.PaperSlip:clone().Parent = Player.Backpack |
3 | end |
4 |
5 | script.Parent.ClickDetector.MouseClick:connect(Clicked) |
Simple!
I wrote this out in the browser and didn't test it.
01 | local RP = game:GetService( "ReplicatedStorage" ) |
02 | local Plr = game:GetService( "Players" ).LocalPlayer |
03 | local BP = Plr:WaitForChild( "Backpack" ) |
04 | local CD = script.Parent:WaitForChild( "ClickDetector" ) |
05 |
06 | CD.MouseClick:connect( function () |
07 | if RP:FindFirstChild( "PaperSlip" ) then |
08 | RP [ "PaperSlip" ] :Clone().Parent = BP |
09 | end |
10 | end ) |
Try this. Also make sure it is called: "PaperSlip". Not something like "Paperslip" or "paperslip".