Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help with cloning?

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

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)

1 comment

0

I had left that i v in pairs loop in there because I was testing with a normal script too.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.