#Speedometer breaks when player resets

1 messages · Page 1 of 1 (latest)

humble acorn
#

I made a small bit of code to track a player's speed, but whenever the player resets, the code still runs, but the gui doesn't update anymore

Code:

local Players = game:GetService("Players")
local runservice = game:GetService("RunService")

local player = Players.LocalPlayer
local playerGui = player.PlayerGui

local ingamePlayer = workspace:WaitForChild(tostring(player))
local HumanoidRootPart = ingamePlayer:WaitForChild("HumanoidRootPart")
local speedUi = playerGui:WaitForChild("SpeedGUI").Frame.SpeedValue



runservice.Stepped:Connect(function()
    speedUi.Text = tostring(math.round(HumanoidRootPart.AssemblyLinearVelocity.Magnitude))
end)
gilded agate
#

Basically, when your character dies and respawns, your script was still pointing at the old HumanoidRootPart.

#

@humble acorn

#

now every time your avatar dies and comes back, your speed‑tracker reconnects and your GUI will keep updating correctly.

#

@humble acorn

faint smelt
#

if you could, maybe use three ` 's at the start and end instead of adding it every line

gilded agate
#
local Players     = game:GetService("Players")
local RunService  = game:GetService("RunService")

local player      = Players.LocalPlayer
local playerGui   = player:WaitForChild("PlayerGui")
local speedUi     = playerGui:WaitForChild("SpeedGUI").Frame.SpeedValue

local humanoidRootPart

local function onCharacterAdded(character)
    humanoidRootPart = character:WaitForChild("HumanoidRootPart")
end

player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
    onCharacterAdded(player.Character)
end

RunService.Stepped:Connect(function()
    if humanoidRootPart then
        local speed = humanoidRootPart.AssemblyLinearVelocity.Magnitude
        speedUi.Text = tostring(math.round(speed))
    else
        speedUi.Text = "0"
    end
end)
#

like this?

faint smelt
#

yah C:

#

it makes it.............. awesome i think.............

humble acorn
humble acorn
#

Been testing it out and it still doesn't update the players speed gui after a reset

#
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local localPlayer = Players.LocalPlayer
local playerGui = localPlayer.PlayerGui

local ingamePlayer = workspace:WaitForChild(tostring(localPlayer))
local HumanoidRootPart
local speedUi = playerGui:WaitForChild("SpeedGUI").Frame.SpeedValue

local function onCharacterAdded(character)
    HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
    print("Found HRP")
end

localPlayer.CharacterAdded:Connect(onCharacterAdded)
if localPlayer.Character then
    onCharacterAdded(localPlayer.Character)
    print("Player", localPlayer, "has joined")
end


RunService.Stepped:Connect(function()
    local speed = HumanoidRootPart.AssemblyLinearVelocity.Magnitude
    if HumanoidRootPart then 
        speedUi.Text = tostring(math.round(speed))
    else
        speedUi.Text = "0"
    end
end)

its strange because how I have the gui set it will show a hashtag as a placeholder for a number value, but when the player resets it doesn't change the placeholder tag. It just stays there...

#

could it be due to the location cuz i have this script inside starterplayerscript