Respawn Script Respawning Twice

So I have no idea why when the timer is not 0 the player gets respawned but when it also hits 0 it also respawns. Also I dont know why the animation is not playing when it worked the same in other scripts

Video: (timer i forgot to make visable)

Server script:

local Players = game:GetService("Players")
local Handler = game:GetService("ReplicatedStorage"):WaitForChild("Handler")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character.Humanoid or Character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
			for _, Object in pairs(Humanoid:GetChildren()) do
				if Object:IsA("ObjectValue") and Object.Value and Object.Value:IsA("Player") then
					Handler:FireClient(Player, "Died", Object.Value.Name)
					Handler:FireClient(Player, "KillCam", Object.Value)
				end
			end
			Handler:FireClient(Player, "Died", "Reset")
		end)
	end)
	Player:LoadCharacter()
end)

Handler.OnServerEvent:Connect(function(Player, Argument1, Argument2)
	if Argument1 == "ResetPlayer" then
		Player:LoadCharacter()
	end
end)

Local script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Handler = ReplicatedStorage.Handler
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local RunServ = game:GetService("RunService")
local Pos = script.Parent.DeathGui.Position

local died = false
local DeathGui = script.Parent:WaitForChild("DeathGui")

function DiedScreen(Killer)
	local Gui = DeathGui:Clone()
	Gui.Parent = DeathGui.Parent
	Gui.KilledBy.Text = "Killed by: "..Killer
	
	Gui.Size = UDim2.new({0.032, 0},{0.011, 0}) 
	Gui.Visible = true
	Gui:TweenSize(UDim2.new(0.443, 0,0.156, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint, 0.175, true)
	
	RunServ.RenderStepped:Connect(function(dt)
		local BobbleX = math.abs(math.cos(os.clock() * 50) * 0.001) 
		local BobbleY = math.abs(math.sin(os.clock() * 20) * 0.001) 
		Pos = Pos + UDim2.new(BobbleX,0,BobbleY,0) 
	end)
	
	task.wait(.5)
	local Time = 5
	repeat
		Gui.RespawnTimer.Text = "Respawning in "..Time 
		task.wait(1)
		Time -= 1
	until Time <= -1
	Handler:FireServer("ResetPlayer")
	Gui:TweenSize(UDim2.new({0.032, 0},{0.011, 0}), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.175, true)
	Gui:Destroy()
	died = false
end


Handler.OnClientEvent:Connect(function(Argument, Argument2)
	if Argument == "Died" and not died then
		died = true
		DiedScreen(Argument2)
	elseif Argument == "KillCam" then
		workspace.CurrentCamera.CameraSubject = Argument2.Character.Head

	end
end)
1 Like

I think I found the issue, I think you forgot to disable automatic respawning. I followed down the script’s logic, and while it looks like you have some memory leak issues as well, just looking at the GUI as a guide for when the local script was done, it looks like the first respawn is just Roblox’s respawn, and the second is the script, where it also deletes the GUI. Just go to players, and set CharacterAutoLoads to false.

1 Like

Thanks! but the ui that popps up is supposed to shake? it in the code with the bubbleX, buubleY but it is not going do you know why?

1 Like

The local Pos is set to a the position of the deathGUI, as is. You would then want to set script.Parent.DeathGui.Position equal to pos later, since that wouldn’t be done automatically.
As an example: If you set a variable to part.name, the variable will be a string, “part,” and if you change that string, all you did was change a variable, and to change the name, you would have to do part.name = variable.

sorry if i missunderstud it, but how would i set the pos later? then instanly

I dunno I was just trying to explain how variables work a bit, but you don’t really need the variable for this anyway, so you can just directly update the position like this:

RunServ.RenderStepped:Connect(function(dt)
		local BobbleX = math.abs(math.cos(os.clock() * 50) * 0.001) 
		local BobbleY = math.abs(math.sin(os.clock() * 20) * 0.001) 
		script.Parent.DeathGui.Position += UDim2.new(BobbleX,0,BobbleY,0) 
	end)

thats bascily what i put, and i tired that thing before and it did not move at all

wait so, what exactly is meant to shake? I haven’t really messed around with UI too much, so I wasn’t sure exactly what it was meant to do anyway, but I assume its like a vector2 or something, though I don’t know if you can move an entire GUI, and would have to move frames and stuff, but I don’t really know what deathGUI is or what exactly is meant to be shaking.

Its supposed to look like this
Here is the code for it too:

	RunServ.RenderStepped:Connect(function(dt)
		local BobbleX = math.abs(math.cos(os.clock() * 50) * 0.001) 
		local BobbleY = math.abs(math.sin(os.clock() * 20) * 0.001) 
		btn.Position = Pos + UDim2.new(BobbleX,0,BobbleY,0) 
	end)

im not really looking for a camera shake

I think I might see the issue now, you are updating the wrong GUI, try this:

RunServ.RenderStepped:Connect(function(dt)
	local BobbleX = math.abs(math.cos(os.clock() * 50) * 0.001) 
	local BobbleY = math.abs(math.sin(os.clock() * 20) * 0.001) 
	Gui.Position = Pos + UDim2.new(BobbleX,0,BobbleY,0) 
end)

I changed some stuff because I was getting the pos of the non cloned item, Here is the code:

	RunServ.RenderStepped:Connect(function(dt)
		local BobbleX = math.abs(math.cos(os.clock() * 50) * 0.001) 
		local BobbleY = math.abs(math.sin(os.clock() * 20) * 0.001) 
		Gui.Position = Gui.Position + UDim2.new(BobbleX,0,BobbleY,0) 
	end)

But it is looking like this?:

Here is where variables being static helps, if you set Pos to the starting position of it, it will use that as an anchor. I assumed position of the non-cloned GUI and the new GUI would be the same so I kept it as is, though if it isn’t, then just do this:

local Pos = Gui.Position
RunServ.RenderStepped:Connect(function(dt)
	local BobbleX = math.abs(math.cos(os.clock() * 50) * 0.001) 
	local BobbleY = math.abs(math.sin(os.clock() * 20) * 0.001) 
	Gui.Position = Pos + UDim2.new(BobbleX,0,BobbleY,0) 
end)
1 Like

thankyou it worked i forget to get the anchor