Cant clone item to player backpack

i used a script that gives a sword to the player if a datastore value is 1. everything works except the cloning part where it does not clone and parent it to player.

heres the script

game.Players.PlayerAdded:Connect(function(plr)
	task.wait(0.3)
	print(plr.data.playerhassword.Value)
	if plr.data.playerhassword.Value == 1 then
		print("playerhassword = 1!")
		local find = plr.Backpack:FindFirstChild("Basic Sword")
		if not find then
			print("player does not have item but value is 1")
			local cloneditem = game.ReplicatedStorage["Basic Sword"]:Clone()
			cloneditem.Parent = plr.Backpack
		end
	end
end)

all print statements work.

1 Like

This might be due to the fact that this happens instantly upon the player joining, you should check if the players character exists before hand:

game.Players.PlayerAdded:Connect(function(plr)
	task.wait(0.3)
	print(plr.data.playerhassword.Value)
	if plr.data.playerhassword.Value == 1 then
		local Character = plr.Character or plr.CharacterAdded:Wait() -- Checks if the character exists, if not it waits
		print("playerhassword = 1!")
		local find = plr.Backpack:FindFirstChild("Basic Sword")
		if not find then
			print("player does not have item but value is 1")
			local cloneditem = game.ReplicatedStorage["Basic Sword"]:Clone()
			cloneditem.Parent = plr.Backpack
		end
	end
end)

The reason we do this is cause the player gets a new backpack when their character loads in, thus any items in it would be removed.

1 Like

k thanks ill try hopefully it works

2 Likes

it Works!!! Thanks man your a legend

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.