and how do i define it? im sorry im really confused
ok i did but still nothing
30characters
studio file too if you want to
local wfc = game.WaitForChild
local player = game.Players.LocalPlayer
local char = player.Character
local humanoidrootpart = wfc(char, “HumanoidRootPart”)
while true do
wait()
if humanoidrootpart == nil then return end
print(humanoidrootpart.Velocity.Magnitude)
end
while true do
script.Parent.Value = humanoidrootpart.Velocity.Magnitude
wait(1)
end
nuclear i dont know what going on but its still not working
can i send you the studio file so you can check it out?
Just a few tips:
- You can use
spawn(func)
to achieve a similar result as Nuclear’s suggestion:coroutine.resume(coroutine.create(func))
- I would recommend (although wait() works fine) using
RunService.Heartbeat:Wait()
instead ofwait()
in the case of velocity checking since it should stay synced with physics. (Generally physics related things should be synced with Heartbeat in this way for more consistent/reliable results but it’s not necessary) - You may want to merge your prints with the counter to improve performance. If you’re displaying this information on a GUI you can instantly update information from a value using
Value:GetPropertyChangedSignal("Value")
or you can directly update the GUI from your script.
Your script can also be placed in StarterPlayer.StarterCharacterScripts
which will place the script into the player’s character each time they spawn.
local player = game.Players.LocalPlayer
local char = player.Character
local humanoidrootpart = char:WaitForChild("HumanoidRootPart")
while true do
wait()
if humanoidrootpart == nil then return end
print(humanoidrootpart.Velocity.Magnitude)
end
Try this. The original code had the following issues, from my knowledge:
- The
wfc
variable. You did not need to do that. -
local humanoidrootpart = wfc(char, "HumanoidRootPart")
I do not believe that the second argument takes another string; rather, it takes a number, which is the timeout, in seconds.
I’m a bit unsure on how you’re doing movement, but since you mentioned WalkSpeed, the Running event is an event of humanoid that fires when a Humanoids WalkSpeed changes. Speed is even passed as a parameter for the event.
Alternatively, there is a function called GetPropertyChangedSignal which you could use to listen to changes in a part’s property, e.g. velocity. Ideally you would use this in a local script, disconnect the functions when the character dies and reconnect them when the new character spawns. (This would be if you’re not using WalkSpeed)
local chr = pathToCharacter
local part = chr:WaitForChild("Humanoid")
local function VelocityChanged()
-- get part velocity, update gui
end
part:GetPropertyChangedSignal("Velocity"):Connect(VelocityChanged)
I haven’t tested it but this should work.
uhm it still doesnt work. can i send the studio file
Can you provide your current code?
A valid point. I never use it. But how did my code work otherwise? Did it fix or no?
I don’t think you can pass anything other than an integer/float into the second argument of :WaitForChild()
If it works, then why is @isarvety having an issue? I’ll test the code and see what problem he’s having.
I have an issue when placing a LocalScript under StarterPlayerScripts. I’ll test further tomorrow.
Steps:
-
Add a new LocalScript to StarterGui like so:
-
Paste this code inside the LocalScript:
local player = game.Players.LocalPlayer local char = player.Character local humanoidrootpart = char:WaitForChild("HumanoidRootPart") while true do wait() if humanoidrootpart == nil then return end print(humanoidrootpart.Velocity.Magnitude) end
-
Watch the magic
A couple of issues I’d like to raise with your repro.
-
PlayerScripts has superseded placing scripts directly in StarterGui. Handling conditions for new characters is up to the developer.
-
Do yourself and your potential collaborators a favour, teach yourself to use GetService to fetch a service and hold it in a variable.
-
You don’t account for an uninitialised character. If your code runs before a character is present, it will throw an error as your code assumes a truthy return for the WaitForChild statement. You can prevent this by yielding the thread until the required signal is fired and presents your needed item.
-
A while loop has a conditional argument. Returning end if a condition isn’t met isn’t the proper way to terminate a loop, it’d be break. Return does exit out of the loop’s scope but break is the proper operator. In addition, you can just use the conditional part properly1.
1:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local characterRoot = character:WaitForChild("Humanoid")
while characterRoot do
print(characterRoot.Velocity.Magnitude)
wait()
end
Now obviously I have no clue if the code itself in concept works, that’s up for a test to reveal. Not as in my specific code, this whole printing of the character’s velocity.
idk @Regen_erate code works fine for me
First @colbert2677 Is using GetService
Second, he is waiting for the character
That line of code means if the character isn’t loaded then let’s wait for it.
Sometimes this code
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character
local Humanoid = Character:WaitForChild("Humanoid")
May throw errors such as
13:58:49.834 - Players.DevOfLua.PlayerScripts.LocalScript:4: attempt to index a nil value
13:58:49.835 - Stack Begin
13:58:49.836 - Script 'Players.DevOfLua.PlayerScripts.LocalScript', Line 4
13:58:49.836 - Stack End
This is because the character wasn’t loaded yet.
Thanks, I do use :GetService()
all the time to store it in a variable, I made a rhythm game and I had to do that a lot. And yeah, I did think about that if the code ran before the player actually existed it would error. I am already aware of all of this, except for the “break” operator lol I literally thought nothing of it; I just modified (I think) @TheAviator01’s code.
BTW, it threw an error if I placed the LocalScript in StarterPlayerService. Idk why.