function click() game.Lighting["WoodKey"]:Clone().Parent = game.Players.LocalPlayer.Backpack end script.Parent.ClickDetector.MouseClick:Connect(click)
i am makign a RPG game it is in a local script too
Click detectors run server sided. Therefore they can only be run by the server.
To get your script to work you have to change it to a server script and do this:
local ClickDetector = script.Parent.ClickDetector local Key = game.Lighting.WoodKey ClickDetector.MouseClick:Connect(function(Plr) local BP = Plr:WaitForChild("Backpack") Key:Clone().Parent = BP end
And course you may want to add a check so they cant spam the button:
local ClickDetector = script.Parent.ClickDetector local Key = game.Lighting.WoodKey ClickDetector.MouseClick:Connect(function(Plr) local BP = Plr:WaitForChild("Backpack") if not BP:FindFirstChild(Key.Name) then Key:Clone().Parent = BP end end
Both will work second is better so people dont get like 60k keys
correct me if I am wrong but I found a way
local clickDetector = script.Parent:WaitForChild("ClickDetector") clickDetector.MouseClick:connect(function(plr) local mdl = game.Lighting.sample:Clone() mdl.Parent = plr.Backpack end)
u need to reference the plr.
script.Parent.ClickDetector.MouseClick:Connect(function(plr) local player=game:GetService("Players"):FindFirstChild(plr.Name) if player then local clone=game.ReplicatedStorage.test:Clone() clone.Parent=player.Backpack print("Added") end end)
So a LocalScript
would not work with a ClickDetector
. So what you want to do is firstly insert a Part
into the Workspace, after that, inside of that part, place a ClickDetector
. Now we have our clicking base done, however we want to run script so finally create a regular script and place it inside of the part.
Now instead of having your WoodKey in Lighting
, place it into ReplicatedStorage
. Lighting is used to change the time of day, implement skyboxes, and just everything to do with Lighting. So in order for the Key to actually appear in the Backpack, you need to press CTRL-I and search up Tool
, double click it and drag it into ReplicatedStorage, where the key is. Rename the Key to Handle
and Rename the Tool to WoodKey
, this is done so it actually appears and we can use the key in our Backpack. Now that we have got that out of the way, let's continue.
ReplicatedStorage holds anything in it's storage, as it says.
Open the script inside of your part and we'll begin. We need to firstly define some variables.
rs = game:GetService("ReplicatedStorage") -- gets the ReplicatedStorage from the game cd = script.Parent:WaitForChild("ClickDetector") -- waits for ClickDetector to load in
Now with a variable, a lot of people add local
when it is not needed. Local is only and mainly needed when you're creating a new object or Instance. If you're defining something that is already a thing; then you do not need a local at the start of your variable. As you can see, I have quoted some of the script to provide an easy understanding.
rs = game:GetService("ReplicatedStorage") cd = script.Parent:WaitForChild("ClickDetector") cd.MouseClick:Connect(function(plr) -- connects to the MouseClick of the ClickDetector end) -- ends the function of the script
We're so close, now we just need to clone the WoodKey tool into our Backpack
!
rs = game:GetService("ReplicatedStorage") cd = script.Parent:WaitForChild("ClickDetector") cd.MouseClick:Connect(function(plr) local key = rs:WaitForChild("WoodKey") -- waits for WoodKey to load local clonedKey = key:Clone() -- clones the key variable clonedKey.Parent = plr:WaitForChild("Backpack") -- places it into the Backpack end)
I have found it out myself Sorry i did not accept answers, took me a while but i was experimenting with scripts and i have found it out myself, i have upvoted all of the posts for helping me here is the script feel free to use
function clicked(Player) game.ServerStorage.WoodKey:Clone().Parent = Player.Backpack end script.Parent.ClickDetector.MouseClick:Connect(clicked)