How do I make the player's speed decrease temporarily when they get damaged

Does anyone know how to make a script where when the player get’s damaged their speed decreases? Also, does anyone know how to make the player lose the ability to jump temporarily when they also get damaged? If so then could you please combine both scripts into one. Thx! :+1:

https://chatgpt.com

Yeah. Pretty sure gpt can handle that easily.

Where ever you damage the other enemies humanoid you can simply do:

-- humanoid gets damage before this
humanoid.WalkSpeed = 2 
humanoid.JumpPower = 0
task.wait(1) -- change time to however you like
humanoid.WalkSpeed = 16
humanoid.JumpPower = 50

if you can still jump you might have to use JumpHeight instead of JumpPower

A smart way to implement this would be using BindableEvents. After the character takes damage, make sure to pass that character like this: bindableEvent:Fire(char). Then you can set up a connection that slows the character and sets their jump power to 0 as coolyoshi suggested. For example:

char.Humanoid:TakeDamage(5)
damageEvent:Fire(char)
damageEvent.Event:Connect(function(char)
	if damageDebounce == false then
		damageDebounce = true
		char.Humanoid.WalkSpeed = 2 
		char.Humanoid.JumpPower = 0
		task.wait(1)
		char.Humanoid.WalkSpeed = 16
		char.Humanoid.JumpPower = 50
		damageDebounce = false
	end
end)

Of course the debounce is there to prevent this from running on top of each other, a cooldown of sorts. The reason for using a bindable event for this is that you can put the 2nd code anywhere you’d like, either in the same script or in a different one. It makes managing your game much easier.

I suppose you can do this on the server (template, edit to optimise to your liking)

game.Players.PlayerAdded:Connect(function(plr)
   plr.CharacterAdded:Connect(function(char)
   local hum = char.Humanoid      hum:GetPropertyChangedSignal(“Health”):Connect(function()
         hum.WalkSpeed = (hum.Health/hum.MaxHealth) * 16
      end)
   end)
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.