(HELP) Teddy Bear keeps cloning when friend joins

Hi! This is a bit of a simple error, but is there a way to stop this while loop so the Teddy Bear gets cloned into the backpack only once? Thanks!

LocalScript:

local rs = game:GetService("ReplicatedStorage")
while wait(5) do
	local Players = game:GetService('Players')
	local player = game.Players.LocalPlayer
	
	local children = Players:GetChildren()
	
	for i, child in pairs(children) do
		if player:IsFriendsWith(child.UserId) then
		rs["Teddy Bear"]:Clone().Parent = player.Backpack
		end
		break
	end
end

Move it outside the for loop into the while loop, and it’s probably better to use a less frequent check, probably using an event.

Try this:

local bear = game.ReplicatedStorage:WaitForChild("Teddy Bear")

for i,v in pairs(game.Players:GetPlayers()) do
     if game.Players.LocalPlayer:IsFriendsWith(v.UserId) then
        bear:Clone()
        bear.Parent = game.LocalPlayer.Backpack
     end
end

game.Players.PlayerAdded:Connect(function(plr)
    if game.Players.LocalPlayer:IsFriendsWith(plr.UserId) then
       bear:Clone()
       bear.Parent = game.LocalPlayer.Backpack
    end
end)
1 Like

You should really use the PlayerAdded or CharacterAdded events instead of a constant loop

1 Like