Hello. I am currently making a speedometer on roblox, but there are multiple problems. First of all, when the character respawns for some reason this script does not detect it?
while wait() do
local char = workspace:WaitForChild(plr.Name)
if char then
repeat wait() until char:FindFirstChild("Jetpack") ~= nil
print("yield over")
if char:WaitForChild("Jetpack").JetpackForce.Enabled == true then
print("passed")
local ySpeed = char:WaitForChild("HumanoidRootPart").AssemblyLinearVelocity.Y
if ySpeed >= 0 then
local max = speed.Value
local oneDegree = max /180
degree = (ySpeed / oneDegree) - 90
if tween then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Pause()
local goal = {Rotation = degree}
tween = ts:Create(pointer,tweeningInfo,goal)
tween:Play()
else
local goal = {Rotation = degree}
tween = ts:Create(pointer,tweeningInfo,goal)
tween:Play()
end
else
local goal = {Rotation = degree}
tween = ts:Create(pointer,tweeningInfo,goal)
tween:Play()
end
elseif ySpeed < 0 then
degree = -90
if tween then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Pause()
local goal = {Rotation = degree}
tween = ts:Create(pointer,tweeningInfo,goal)
tween:Play()
end
end
end
else
degree = -90
if tween then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Pause()
local goal = {Rotation = degree}
tween = ts:Create(pointer,tweeningInfo,goal)
tween:Play()
end
end
end
else
char = plr.Character or plr.CharacterAdded:Wait()
end
end
I am not sure why this does not detect the newly spawned part in my character - it does not print (“yield over”) once I respawn, and I am 100% sure that I do have that part in my character. No idea why this isn’t working, help will be much appreciated.
P.S Any adjustments to the script to make it run smoother and better are welcome too.
This code needs to be rewritten entirely. Connect the game.Players.LocalPlayer.CharacterAdded event to a function that controls your speedomoter with a loop. Then when a new character spawns, end your old loop and let the new one start using the new character model.
local charId = 0
game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
charId += 1
local thisCharId = charId
while thisCharId == charId and wait() do
-- Your code to set speedomoter
end
end)
I edited the example code, use the new version. Your code would theoretically work but it is very redundant and inefficient. You are using three different ways to get the character and an infinite loop to detect character respawning.
local plr = game.Players.LocalPlayer
local pointer = script.Parent
local speed = plr:WaitForChild("leaderstats").Speed
local ts = game:GetService("TweenService")
local gl = {}
local tween
local tweeningInfo = TweenInfo.new(0.1)
local degree
local charID = 0
plr.CharacterAdded:Connect(function(char)
print(1)
local jetpack = char:WaitForChild("Jetpack")
local HRP = char:WaitForChild("HumanoidRootPart")
charID += 1
local thisCharID = charID
if thisCharID == charID then
print(2)
game:GetService("RunService").RenderStepped:Connect(function()
local ySpeed = HRP.AssemblyLinearVelocity.Y
print(ySpeed)
if jetpack.JetpackForce.Enabled == true then
if ySpeed >= 0 then
local max = speed.Value
local oneDegree = max /180
degree = (ySpeed / oneDegree) - 90
if tween then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Pause()
local goal = {Rotation = degree}
tween = ts:Create(pointer,tweeningInfo,goal)
tween:Play()
else
local goal = {Rotation = degree}
tween = ts:Create(pointer,tweeningInfo,goal)
tween:Play()
end
else
local goal = {Rotation = degree}
tween = ts:Create(pointer,tweeningInfo,goal)
tween:Play()
end
else
degree = -90
if tween then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Pause()
local goal = {Rotation = degree}
tween = ts:Create(pointer,TweenInfo.new(0.2),goal)
tween:Play()
end
end
end
else
degree = -90
if tween then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Pause()
local goal = {Rotation = degree}
tween = ts:Create(pointer,TweenInfo.new(0.2),goal)
tween:Play()
end
end
end
end)
end
end)
the code I have currently, but for some reason nothing prints? I am very confused currently.
I just did a little testing. If your local script is in StarterGui, it doesn’t detect player.CharacterAdded I believe. Consider putting it in StarterPlayerScripts.
I’m not sure why everyone puts this in their script as it’s usually for the starter GUI scripts. StarterPlayerScripts is where this should be put.
For OP, the code should work if everything is set up correctly, but the script must be placed in StarterPlayerScripts. Additionally, the player might need to reset once before the script takes effect (when joining for the first time).
Through my testing and many years using plr.Character or plr.CharacterAdded:Wait() it works perfectly every time. The reason is that if plr.Character is nil, then it waits for the Character to be added.
Yes, but it’s mainly for scripts placed in starter GUI (back before StarterPlayer even existed) and when the player respawns the character won’t be updated unless more code is added.