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

clone key to local player backpack how?

Asked by 4 years ago
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

0
why is a tool in lighting EmbeddedHorror 299 — 4y
0
theres replicatedstorage/serverstorage for a reason EmbeddedHorror 299 — 4y
0
ik i like using lighting idk why Vortex_Vasne 89 — 4y

5 answers

Log in to vote
2
Answered by 4 years ago

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

0
and nope Vortex_Vasne 89 — 4y
0
Did you put it in a server sided script as i said at the top? MrCatDoggo 213 — 4y
0
best answer, though i would suggest rep stor or serv stor for tools EmbeddedHorror 299 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

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)
0
nope Vortex_Vasne 89 — 4y
Log in to vote
1
Answered by 4 years ago

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)
Log in to vote
1
Answered by 4 years ago

Hello Vortex_Vasne!

I am here to help you with your script!

Let's begin, shall we?

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 hope I have helped you Vortex_Vasne, if I did not please tell me in the comments and I'll try to help you!

If you feel you've learnt something from me or it helped you, please Accept Answer!
Log in to vote
0
Answered by 4 years ago

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)

Answer this question