3 Lines of code only work when debugging

What I’m trying to do is just add a folder into the player to put their items in when they die, so I can get them back and put them back into the player backpack, however the 3 lines of script just WON’T run, expect when I debug them, they do?

I have tried adding waits, when I add prints they do print, it’s just these 3 specific lines of code not working when ran normally

Main Problem:

	local Respawn_Items = Instance.new("Folder")
	
	Respawn_Items.Name = "Respawn_Items"
	Respawn_Items.Parent = Player

Video:

All I can think of is maybe Player is nil by the time that line is reached? This could be a timing issue which would explain why it works in debug.

1 Like

I think you could be right, when I tried adding it this time to replicated storage it does load in, so I tried adding a wait(3) before the line but it still wont load

	wait(3)
	
	local Respawn_Items = Instance.new("Folder")
	
	Respawn_Items.Name = "Respawn_Items"
	Respawn_Items.Parent = Player

Is there a better way to time it?

The wait would need to be before variable Player is assigned.

1 Like

Try delaying Parent assignment:

delay(3, function()
   Respawn_Items.Parent = Player
end)
1 Like

Tried this, still won’t work

Code:

	local Respawn_Items = Instance.new("Folder")
	
	Respawn_Items.Name = "Respawn_Items"
	
	delay(3, function()
		print("Delayed")
		Respawn_Items.Parent = Player
	end)

Output:
image

Player:
image

I’m using playeradded so if I add a wait before the function (where the variable is) it wont think a player was ever added

wait(3)
game.Players.PlayerAdded:Connect(function(Player)

image

Add this inside the delay:

-- make sure the player is present even after delay
if not Player then repeat wait(1) until Player
1 Like

Player is present when this is ran, and it does detect the player, because delayed prints in the output

	delay(3, function()
		if not Player then repeat wait(1) until Player end
		print("Delayed")
		Respawn_Items.Parent = Player
	end)
1 Like

Alright, I found the problem, I was trying to get the folder before it was loaded in the first place, which I guess bugged it?

I just had to disable that script, thank you all for help!

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