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

How come my clone script doesnt' work?

Hi, what I'm trying to do here is clone a weapon called "M16" to a players backpack. To do this, I try to cilck a Part and then it will clone a Weapon to a players backpack.

	 gunmodel = script.Parent
	 players = game.Players.LocalPlayer
	function onClicked()
		for i = 1, #players do
		if gunmodel ~= nil then
		gun = game.Lighting.Weapons.M16:clone()
		gun.Parent = players.Backpack
		wait(.1)
		script.Parent:Destroy()
		end
		end
		end
	script.Parent.ClickDetector.MouseClick:connect(onClicked)

11 comments

0

Oh yeah, just another tip.. you don't need to use lighting anymore. You can use ServerStorage. It's a better place to store it.

0

There is a problem. It says that "player" is a nil value

0

is this a script inside a brick or a player?

0

It is inside a brick named "Part"

0

So, do you want to give everyone the gun when the brick is clicked? or just the player who clicked it?

0

Very sorry for the late reply, I'm trying to give it to the player who clicked it, not everyone.

0

there, i fixed it for you. it should work fine now

0

Okay, I'll check it out and give you a green check mark

0

if it doesn't work, just tell me where it breaks. I might have got the player situation wrong.

0

Wow, it works, thanks for sticking with me to the end.

0

Okay, I'll check it out and give you a green check mark

1 answer

✓ Accepted answer
Gun = game.Lighting.Weapons:FindFirstChild("M16") -- You first need to locate what you want to clone outside of the function.
function onClicked(player)
    ClonedGun = Gun:clone() -- clone what you want and give it another name.
    ClonedGun.Parent = player.Backpack -- put what you cloned inside the backpack of the player
    wait(.1)
    script.Parent:Destroy() -- delete this if you want anyone who clicks on the brick to get the gun.
    end
    end
    end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

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