When a player enters a zone like a part zone or something I want their speed to be limited to like about half but I don’t know how to do this because I have tried using velocity and math.clamp but it did not work and I floated in the air.
Is this possible to do without using WalkSpeed?
Re-clarification:
I want to be able to control the speed that the player moves without using walkspeed, I want to be able to set the max speed I want (studs per second) and I want it to be somewhat exact. its like walkspeed but only not walkspeed, using some other thing like a bodymover or velocity. I do not know how to do this help is and will be appreciated!
You can set their WalkSpeed, which is a property of Humanoid.
You can read more about it here.
Here is a code sample for now:
part.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if (plr == nil) then return end
local char = plr.Character
if (char:FindFirstChild("Humanoid") == nil) then return end
if (char.Humanoid.Health == 0) then return end
char.Humanoid.WalkSpeed = 8 -- the default walkspeed is 16, so 8 is half of 16
end)
I will edit the post with a gif showing the result.
As you see, when you touch the part, it slows them down.
Here is another example that uses TouchEnded that works like a zone:
local function check(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if (plr == nil) then return end
local char = plr.Character
if (char:FindFirstChild("Humanoid") == nil) then return end
if (char.Humanoid.Health == 0) then return end
return char
end
local part = game.Workspace.Part
part.Touched:Connect(function(hit)
local char = check(hit)
if (char == nil) then return end
char.Humanoid.WalkSpeed = 8 -- the default walkspeed is 16, so 8 is half of 16
end)
part.TouchEnded:Connect(function(hit)
local char = check(hit)
if (char == nil) then return end
char.Humanoid.WalkSpeed = 16 -- return them to the default walkspeed
end)
This is the script I have got so far but it does not work am I doing something wrong?
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local LocalPlayer = Players.LocalPlayer
wait(5)
local GetMass = function(Model)
local Mass = 0
for _, Value in pairs(Model:GetDescendants()) do
if Value:IsA("BasePart") then
Mass = Mass + Value:GetMass()
end
end
return Mass * workspace.Gravity
end
local Mass = GetMass(LocalPlayer.Character)
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.MaxForce = Vector3.new(Mass, Mass, Mass)
BodyVelocity.Parent = LocalPlayer.Character.PrimaryPart
while true do
BodyVelocity.MaxForce = Vector3.new(Mass, Mass, Mass)
BodyVelocity.Velocity = -LocalPlayer.Character.PrimaryPart.Velocity
RunService.RenderStepped:Wait()
end
I’m wrong, sorry about that! I had forgotten how BodyVelocities work. Here is some code:
local MAX_SPEED = 20
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local LocalPlayer = Players.LocalPlayer
wait(5)
local humanoidRootPart = LocalPlayer.Character.PrimaryPart
while true do
local velocity = humanoidRootPart.AssemblyLinearVelocity
if velocity.Magnitude > MAX_SPEED then
local mass = humanoidRootPart.AssemblyMass
local impulse = (velocity.Unit*MAX_SPEED - velocity)*Vector3.new(mass, mass, mass)
humanoidRootPart:ApplyImpulse(impulse)
end
RunService.Stepped:Wait()
end
It might not work well with characters though, the character controller tends to override physics changes.
I am not looking to overhaul the whole movement system I am just looking to limit the players speed like changing walkspeed from 16 to 8 just using something else other than WalkSpeed for example Velocity but that seems to be a dead end so far
What’s the point not to use walkspeed? It just seems like a route to a long time of code; is there a reason for this? Because if you want to slow down the player, I would use @RecanValor’s method. Can you enlighten me on why you don’t want to use walkspeed@xDeltaXen?
WalkSpeed is not an instant change in walkspeed I ned it to go from like Velocity.Magnitude = 16 to Velocity.Magnitude = 8 instantly but if I change walkspeed then It will not be an instant change it will be a quick change but it wont instantly go from a Magnitude of 16 to a magnitude of 8.
then just set the velocity.magnitude of the character to 0 and set the walkspeed to 0 and then after you do that instantly set the walkspeed to the desired speed