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

Determining a Velocity/Speed?

Right. Im creating a animation script for a RPG enemy, (and I know it is not correct), and that I cant figure out what I need to find out the speed/or at least how to tell whether the enemy is moving, so when he is moving he "runs" and when he is not, he "Stands". If you use a free model script, the animation for a RPG enemy works fine, in solo mode. However when going to normal (multiplayer) mode it decides to not work. idk if this is to do with servers/clients etc, because I don't understand them. Is there something else I need to do? :

function onRunning()
	if isSeated then return end

	if script.Parent.Torso.Velocity > (0,0,0) then -- A single value wont work
		pose = "Running"
	else
		pose = "Standing"
	end
end

Thanks ~Bubs

2 answers

✓ Accepted answer

If you're using the Running event, you can receive the speed of the character by its argument.

Humanoid.Running:connect(function (speed)
	if speed > 20 then
		print("Wow, you're fast!")
	else
		print("Wow, you're slow.")
	end
end)

You could also calculate the speed by using the HumanoidRootPart velocity's "x" and "z" components with Pythagorean Theorem.

HRP = script.Parent.HumanoidRootPart

function PythagoreanTheorem(a, b)
	local solution = math.sqrt(math.pow(a, 2) + math.pow(b, 2))
	return solution
end

function Example()
	if PythagoreanTheorem(HRP.Velocity.x, HRP.Velocity.z) > 20 then
		print("Wow, you're fast!")
	else
		print("Wow, you're slow.")
	end
end

Why not use the Torso's velocity components? Because, in my perspective, animations can make the velocity of the torso go faster.

4 comments

0

Great Stuff, Thanks! :)

0

Ah. This Actually Caused a Problem :'( . Like i said earlier, this only works in edit mode, solo. now in the actual game? Any Ideas? ( Running event)

0

Any errors in the output?

0

none at all - i would hapily uncopylock if you wish?

If your enemy has a Humanoid in it, you can use the Running event of the Humanoid to determine whether they are running or not.

The running event has a single number parameter, speed. This variable tells you how fast the humanoid is running when the event is triggered.

The running event is also useful to you because this event is triggered when the player stops moving too, where the speed parameter becomes 0.

For your script above, you can do the following to accomplish what you're doing:

script.Parent.Humanoid.Running:connect(function(speed) --Change the path so it points to the humanoid of the enemy.
	if isSeated then return end --I suppose you need this so I kept it in just in case.
	if speed > 0 then --If the enemy is running then set the pose.
		pose = "Running"
	else
		pose = "Standing"
	end
end)

2 comments

0

Don't you just hate when people ninja you? Sorry, Red.

0

Ah. This Actually Caused a Problem :'( . Like i said earlier, this only works in edit mode, solo. now in the actual game? Any Ideas?

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