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

This .Died function runs once but never again?

This died function on line 15 runs once but never again, it warns 4 one time but never again after that, does anyone know why that would be?

game.Players.PlayerAdded:Connect(function(plr)
if game.Workspace:WaitForChild("TrapBasics") then
for i, v in pairs(game.Workspace.TrapBasics:GetChildren()) do
	if v.Name == "TrapBasic" then
		if v.Dedication.Player.Value == "" then
			v.Name = plr.Name
            v.Dedication.Player.Value = plr.Name
            break
		end
end
end
end
local char = plr.CharacterAdded:Wait()
char.Archivable = true
char:WaitForChild("Humanoid").Died:Connect(function() -- Here
	print("1")
	 plr.leaderstats.Score.Value = 0
	wait(6)
	print("2")
		game.Workspace.TrapBasics[plr.Name]:Destroy()
		wait(1)
		print("3")
		local level = game.ServerStorage.TrapBasic:Clone()
		level.Parent = game.Workspace.TrapBasics
		level.Name = plr.Name
		level.Dedication.Player.Value = plr.Name
		level:SetPrimaryPartCFrame(game.Workspace.TrapBasics:FindFirstChild(plr.Name).PrimaryPart.CFrame)
		warn("4")
		return
end)
end)

Thanks!

1 answer

✓ Accepted answer

Hello.


The old character gets deleted when it respawns. To fix this, use the CharacterAdded event.

It will fire whenever a player's character is added.


Fixed Script:

game.Players.PlayerAdded:Connect(function(plr)
if game.Workspace:WaitForChild("TrapBasics") then
for i, v in pairs(game.Workspace.TrapBasics:GetChildren()) do
    if v.Name == "TrapBasic" then
        if v.Dedication.Player.Value == "" then
            v.Name = plr.Name
            v.Dedication.Player.Value = plr.Name
            break
        end
end
end
end

plr.CharacterAdded:Connect(function(char)
	char.Archivable = true
	char:WaitForChild("Humanoid").Died:Connect(function() -- Here
	    print("1")
	     plr.leaderstats.Score.Value = 0
	    wait(6)
	    print("2")
	        game.Workspace.TrapBasics[plr.Name]:Destroy()
	        wait(1)
	        print("3")
	        local level = game.ServerStorage.TrapBasic:Clone()
	        level.Parent = game.Workspace.TrapBasics
	        level.Name = plr.Name
	        level.Dedication.Player.Value = plr.Name
	        level:SetPrimaryPartCFrame(game.Workspace.TrapBasics:FindFirstChild(plr.Name).PrimaryPart.CFrame)
	        warn("4")
	        return
	end)
	
	
end)

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