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

Help with cloning?

Asked by
FiredDusk 1466 Moderation Voter
9 years ago

Wep does not get parented into Lighting when I click. What I am trying to do is, when someone clicks they get the "Wep" but clones into Lighting so other people can get it.

Part = script.Parent

Part.ClickDetector.MouseClick:connect(function()
    local Wep = game.Lighting:WaitForChild("Sword")
    Wep:Clone().Parent = game.Lighting
    local Player = game.Players.LocalPlayer
    for i,v in pairs (game.Players:GetPlayers()) do
        Wep.Parent = Player.Backpack
    end
end)

2 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You're thinking about this weirdly.

Instead of taking it out of lighting, and putting a clone in its place, just never take it out of lighting -- only move clones.


There's also no reason for the for loop. You don't use i or v -- you should only be caring about the local player.

Part = script.Parent

Part.ClickDetector.MouseClick:connect(function()
    local Wep = game.Lighting:WaitForChild("Sword")
    local Player = game.Players.LocalPlayer
    Wep:Clone().Parent = Player.Backpack
end)
0
I had left that i v in pairs loop in there because I was testing with a normal script too. FiredDusk 1466 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

The issue is that you've cloned the weapon once, and you are trying to set the parent of one object to many parents.

If you're trying to give every player a weapon when someone clicks the part, try this:

Part = script.Parent

Part.ClickDetector.MouseClick:connect(function()
    for i,v in pairs (game.Players:GetPlayers()) do
     local Wep = game.Lighting:WaitForChild("Sword"):Clone()
     Wep.Parent = v
    end
end)

If you're trying to give the player who clicked the part a weapon, try this code:

Part = script.Parent

Part.ClickDetector.MouseClick:connect(function(player)
     local Wep = game.Lighting:WaitForChild("Sword"):Clone()
     Wep.Parent = player
end)

Find more info on in pairs and :clone() here: http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions&redirect=no#pairs http://wiki.roblox.com/index.php?title=API:Class/Instance/Clone

You should also check out MouseClick, and defining the player who clicked here: http://wiki.roblox.com/index.php?title=API:Class/ClickDetector/MouseClick

Answer this question