Player not teleporting after death

Code:

local model = game:GetService('Workspace').Checkpoints

for i,v in pairs(model:GetChildren()) do
	if v:IsA("BasePart") then
		v.Name = i
	end
end
local name = "Banna"
local showname = "Checkpoint"
local DSService = game:GetService('DataStoreService'):GetDataStore(name)
game.Players.PlayerAdded:Connect(function(plr)
	local uniquekey = 'id-'..plr.userId
	local leaderstats = Instance.new('IntValue', plr)
	local savevalue =  Instance.new('IntValue',leaderstats)
	leaderstats.Name = 'leaderstats'
	savevalue.Name = showname
	savevalue.Value = 1

	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
	else
		local NumbersForSaving = {savevalue.Value}
		DSService:SetAsync(uniquekey, NumbersForSaving)
	end
	for i,v in pairs(game:GetService('Workspace').Checkpoints:GetChildren()) do
		if v:IsA("BasePart") then
			if tonumber(savevalue.Value) >= tonumber(v.Name) then
				v.BrickColor = BrickColor.new("Medium green")
			else
				v.BrickColor = BrickColor.new("Persimmon")
			end
		end
	end
	plr.CharacterAdded:Connect(function(chr)
		chr.Humanoid.Died:Connect(function()

			print(plr.Name.." has died.")
			local l =  plr:FindFirstChild("leaderstats")
			local savev = l:FindFirstChild(showname)
			plr.CharacterAdded:Wait()
			chr:FindFirstChild("HumanoidRootPart").Position = game:GetService('Workspace').Checkpoints:FindFirstChild(savev.Value).Position
			
		end)

		if plr:FindFirstChild("leaderstats"):FindFirstChild(showname).Value == 0 then
			plr:FindFirstChild("leaderstats"):FindFirstChild(showname).Value = 1
		end
		
		for i,v in pairs(model:GetChildren()) do
			if v:IsA("BasePart") then
				v.Touched:Connect(function(h)
					if h.Parent:FindFirstChild("Humanoid") then
						local l =  plr:FindFirstChild("leaderstats")
						local savev = l:FindFirstChild(showname)
						if tonumber(v.Name) > savev.Value then
							savevalue.Value = tonumber(v.Name)
						end
					end
				end)
			end
		end
	end)

	game.Players.PlayerRemoving:connect(function(plr)
		local uniquekey = 'id-'..plr.userId
		local Savetable = {plr.leaderstats[showname].Value}
		DSService:SetAsync(uniquekey, Savetable)	
	end)
end)
1 Like

You should not consider Player.CharacterAdded and Humanoid.Died on top of each other but only Player.CharacterAdded. Consider that event as the respawn process.

How would I detect when the player has respawned though?

I believe I see three issues with the code that keep the player from teleporting. All can be seen from this block of code:

The first is that you are trying to teleport the character model that has despawned. the event CharacterAdded returns the character model that is added, so you should set that to a local variable.

The second thing is that you do not wait for the game to load and initialize the character into the workspace before trying to teleport. To avoid this, I would add a task.wait() before teleporting.

The last thing is that you are trying to change the HumanoidRootPart’s position property. Instead, you should change HumanoidRootPart’s CFrame property.

So in the end, that block of code should look like this instead:

local newChr = plr.CharacterAdded:Wait()
task.wait()
newChr:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(game:GetService('Workspace').Checkpoints:FindFirstChild(savev.Value).Position)

Unless there are other errors within the script, I believe this should teleport the player

Hope this helps!

3 Likes

Thank for the suggestion. Testing if it works. :slight_smile:

It works! Been trying to fix this all day.

1 Like