For loop runs twice?

So I have started to work on the player tools in my game, and I would like them to load into the Backpack after my loading screen has been done loading. The script works fine, but I have run into a problem which I do not understand. When the script gives the tools, it runs the for loop twice (I get x2 the gears that I’m supposed to get).

Here is my code for the tool giving:

for a, Item in pairs(Player.PlayerItems:GetChildren()) do
	wait()
	if Item.Innocent.Value == true then
		Item:Clone().Parent = Player.Backpack
	end
end

Innocent is a value which I have added to all items. It indicates if the tool should be given or not inside the lobby (If it’s false, then it’s harmfull to other players and the lobby). Player is a local variable created in the begining of the script.

Anyone have any ideas?

well I guess u have two tools with Innocent value true… try to print Item.Innocent.Value in the start of for loop

1 Like

This is where it get’s the item from

image

As you can see, there is only one object there, and it contains only one “Innocent” bool value

for a, Item in pairs(Player.PlayerItems:GetChildren()) do
	print(a)
        print(Item)
        wait()
	if Item.Innocent.Value == true then
		Item:Clone().Parent = Player.Backpack
	end
end

I am intrested what u get

1 Like

hmmm… is this the only code u have in that script? if not, then give me more

1 Like

This is the whole script:

local Player = game.Players.LocalPlayer
local LoadingText = script.Parent.Background.LoadingText
local Frame = script.Parent.Background

repeat wait() 
LoadingText.Text = "Loading" .. "."
wait(0.5)
LoadingText.Text = "Loading" .. ".."
wait(0.5)
LoadingText.Text = "Loading" .. "..."
wait(0.5)
until game:IsLoaded()


LoadingText.Text = "Done Loading!"
wait(3)
Frame:TweenPosition(UDim2.new(1.1, 0, 0, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)
Player.InLoadingScreen.Value = false
for i = 1, 50 do
	wait(0.01)
	for _, Music in pairs(workspace.GameSounds:GetChildren() and workspace.GameSounds:GetDescendants()) do
		if Music:IsA("Sound") then
			Music.Volume = Music.Volume + 0.01
		end
	end
end
for a, Item in pairs(Player.PlayerItems:GetChildren()) do
	print(a)
        print(Item)
        wait()
	if Item.Innocent.Value == true then
		Item:Clone().Parent = Player.Backpack
	end
end
wait(1)
game.ReplicatedStorage.SetPlayerStat:FireServer(Player.InLoadingScreen.Value, false)
script.Parent:Destroy()

The script is basically the gui while it’s loading, then when it’s done, it turns up the game volume. None of it should affect the script I believe?

Where and when does the PlayerItems folder/its contents get added?

1 Like

ok I dont see a problem here. There might be a chance u have duplicated scripts, search all the scripts in explorer, also if the script is located inside of the tool, then its obviously cloned

1 Like

The folder is created when the player joins by PlayerAdded script. The content is filled by a playerscript which checks what items the person own when it has joined.

And there is no way for that script to somehow run twice?

1 Like

The code snippet you provided isn’t enough to debug your issue. Can you provide the rest of the script?

1 Like

I just found the problem, I had it on inside the gui in ReplicatedFirst, so it would run inside both ReplicatedFirst and PlayerGui

Thanks for the help!

2 Likes