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

How to retrieve player position after death?

I'm currently working on a LocalScript which involves the Position of the LocalPlayer. The issue is that as soon as the player respawns, its position stops updating. What i've tried to do is to stop the while playerAlive loop on death and refresh the humanoid value on respawn.

Here is the current version of my script:

local SoundService = game:GetService("SoundService")
local Players = game:GetService("Players")
local backgroundMusic = SoundService.BackgroundMusic
local volume = backgroundMusic.Volume
local distance
local maxVol = 1
local spread = 5*10/maxVol

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hmndrootpart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local playerAlive = true

backgroundMusic:Play()

while true do
	character:WaitForChild("Humanoid").Died:Connect(function() --check for death
		playerAlive = false
	end)
	if humanoid == nil then --refresh humanoid if the player is dead
		player = game:GetService("Players").LocalPlayer
		character = player.Character or player.CharacterAdded:Wait()
		hmndrootpart = character:WaitForChild("HumanoidRootPart")
		humanoid = character:WaitForChild("Humanoid")
	end
	if humanoid ~= nil then --start loop if the humanoid is not nil
		playerAlive = true
	end 
    while playerAlive do -the loop that is supposed to stop on death
	    local varx = hmndrootpart.Position.X --freezes on respawn
	    print ("X=", varx)
	    local vary = hmndrootpart.Position.Y --freezes on respawn
	    print ("Y=", vary)
	    local varz = hmndrootpart.Position.Z --freezes on respawn
	    print ("Z=", varz)
	    distance = math.sqrt((varx^2+vary^2+varz^2)/100)
	    print ("Distance=", distance)
	    backgroundMusic.Volume = maxVol-distance/spread
	    print ("Volume=", backgroundMusic.Volume)
	    wait(0.1)
	end
end

The LocalPlayer position still keeps freezing and the while playerAlive loop never stops. I've been trying to get it to work for hours and I'm out of ideas.

1 answer

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local dead = false
		char.Humanoid.Died:Connect(function()
			dead = true
		end)
		while not dead do
			print(char.HumanoidRootPart.Position)
			wait()
		end
	end)
end)

This code continues to print the Player's position. It stops when the player dies and restarts when it respawns.

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