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

script breaks after you die/reset?

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
	repeat wait() until plr.Character:FindFirstChild("Humanoid")
	while wait() do
	if char:FindFirstChild("ForceField") then
				for _,c in pairs(plr.Backpack:GetChildren()) do
				local bp = c:Clone()
				c:Destroy()
				bp.Parent = plr.PlayerGui.tools
				end
				else
				for _,v in pairs(plr.PlayerGui.tools:GetChildren()) do
				local pb = v:Clone()
				v:Destroy()
				pb.Parent = plr.Backpack
				end
		end
	end
	char.Humanoid.Died:connect(function()
				for _,c in pairs(plr.Backpack:GetChildren()) do
				local bbp = c:Clone()
				c:Destroy()
				bbp.Parent = plr.PlayerGui.tools
				end
				end)
end)
end)


--Broken, it spams after u reset.

I made this script, basically what it does is: if you are inside a forcefield, it clones the items from ur inv, puts them in a folder and the deletes the one in ur inventory. if you are outside the forcefield then it puts your items back in ur inv and deletes them from the folder.

The problem is: it works perfectly until you reset/die, once you die and respawn the script starts to spam the items in your inventory like this: https://gyazo.com/59e1b824d31b546f92898e3e789514e2

1 comment

0

HEY THATS BEEN HAPPENING TO ME TO!! NO WAY!! I NEED THIS ANSWERED TOO. I will try to get your question noticed

1 answer

✓ Accepted answer

You should use ChildAdded and ChildRemoved instead of loops:

local recordedTools = {}
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.ChildAdded:Connect(function(child)
			if child:IsA("ForceField") then
				local tools = plr.Backpack:GetChildren()
				local equippedTool = char:FindFirstChildOfClass("Tool")
				if equippedTool then
					table.insert(tools,1,equippedTool)
				end
				recordedTools = tools
				for _,v in pairs(tools) do
					v.Parent = nil
				end
			end
		end)
		char.ChildRemoved:Connect(function(child)
			if child:IsA("ForceField") and recordedTools then
				for _,v in pairs(recordedTools) do
					v.Parent = plr.Backpack
				end
			end
		end)
	end)
end)

2 comments

0

your script doesn't work.

0

nvm, thank you a lot. the problem is at line 12, you forgot to add "do" thank you a lot dude, it works perfectly

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