Walkspeed Issues

I’m trying to create a walkspeed that changes based on the player’s health. My script currently only lowers the walkspeed of the player when they reach a lower health, but it doesn’t give back their original walkspeed when they regain their health. The debounces are for the stun system where it changes the player’s walkspeed to 0 for a short period. Here’s my script:

local debounce = false
local h = game.ReplicatedStorage:WaitForChild("Halt")
local c = game.ReplicatedStorage:WaitForChild("Continue")

h.Event:Connect(function()
	debounce = true
	print("true")
end)

c.Event:Connect(function()
	debounce = false
	print("false")
end)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		if debounce == false then
			while debounce == false do
				wait()
				if humanoid.Health > 75 then humanoid.WalkSpeed = 30
					print("30")
				end
				if humanoid.Health < 75 and humanoid.Health > 50 then humanoid.WalkSpeed = 18
					print("18")
				end
				if humanoid.Health < 50 then humanoid.WalkSpeed = 5
					print("5")
				end
			end
		end

	end)
end)

you could make a new event for when the health changes

humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if humanoid.Health > 75 then
		humanoid.WalkSpeed = 30
		print("30")
	elseif humanoid.Health < 75 and humanoid.Health > 50 then
		humanoid.WalkSpeed = 18
		print("18")
	elseif humanoid.Health < 50 then
		humanoid.WalkSpeed = 5
		print("5")
	end
end)
1 Like

I tested it but now the walkspeed here overrides the stun system.

you could add a bool value in the player called Stunned and only change the walkspeed if they’re not stunned

humanoid:GetPropertyChangedSignal("Health"):Connect(function()

    if Stunned.Value then return end

	if humanoid.Health > 75 then
		humanoid.WalkSpeed = 30
		print("30")
	elseif humanoid.Health < 75 and humanoid.Health > 50 then
		humanoid.WalkSpeed = 18
		print("18")
	elseif humanoid.Health < 50 then
		humanoid.WalkSpeed = 5
		print("5")
	end
end)

This solved the script overriding the stun system, but it’s back to the same problem as earlier where it doesn’t give back their original walkspeed when they regain their health. I think it was similar to the debounces I added.

Something like this will work:

local stunsub = 10 --how much walkspeed to take off when stunned
local h = game.ReplicatedStorage:WaitForChild("Halt")
local c = game.ReplicatedStorage:WaitForChild("Continue")

h.Event:Connect(function()
	debounce = true
	print("true")
end)

c.Event:Connect(function()
	debounce = false
	print("false")
end)

function setspeed(plr, stunned)
	local walkspeed
	
	if not plr.Character then return end
	
	local humanoid = plr.Character:FindFirstChild("Humanoid")
	
	if not humanoid then return end
	
	if humanoid.Health >= 75 then
		walkspeed = 30
	elseif humanoid.Health < 75 and humanoid.Health >= 50 then
		walkspeed = 18
	elseif humanoid.Health < 50 then
		walkspeed = 5
	end

	if stunned.Value then
		walkspeed -= stunsub
	end
	
	walkspeed = walkspeed > 0 and walkspeed or 0
	
	humanoid.WalkSpeed = walkspeed
end

game.Players.PlayerAdded:Connect(function(player)
	local stunned = --stunned value (move into CharacterAdded if under character)
	
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		
		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			setspeed(plr, stunned)
		end)
	end)
	
	stunned:GetPropertyChangedSignal("Value"):Connect(function()
		setspeed(plr, stunned)
	end) -- also move into characteradded if stunned under character

Nice to meet you again! From what I can see (if I’m right), I think you added a stunned system to the script. Right now I just need a movement system because I already added a stunned system in another script. If the bottom part was where you set the stunned value to the player, I think the part could be removed right?

I believe merging the scripts is the best thing to do currently. Considering if you try to include the stunning system in another script and the health walkspeed in a different one, it’ll make the stun ineffective. Nice to meet you again too!

I’m currently trying to merge the scripts together, but I’m trying not to change too much of your original script. If possible, could you show me what I should I change? (I’m trying to remove the stunned value in the script.)

It depends. As I have commented out, if the stunned value is within the character, then the best thing to do would be this:

local stunsub = 10 --how much walkspeed to take off when stunned
local h = game.ReplicatedStorage:WaitForChild("Halt")
local c = game.ReplicatedStorage:WaitForChild("Continue")

h.Event:Connect(function()
	debounce = true
	print("true")
end)

c.Event:Connect(function()
	debounce = false
	print("false")
end)

function setspeed(plr, stunned)
	local walkspeed
	
	if not plr.Character then return end
	
	local humanoid = plr.Character:FindFirstChild("Humanoid")
	
	if not humanoid then return end
	
	if humanoid.Health >= 75 then
		walkspeed = 30
	elseif humanoid.Health < 75 and humanoid.Health >= 50 then
		walkspeed = 18
	elseif humanoid.Health < 50 then
		walkspeed = 5
	end

	if stunned.Value then
		walkspeed -= stunsub
	end
	
	walkspeed = walkspeed > 0 and walkspeed or 0
	
	humanoid.WalkSpeed = walkspeed
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		local stunned = --stunned value (move into CharacterAdded if under character)
		
		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			setspeed(plr, stunned)
		end)

		stunned:GetPropertyChangedSignal("Value"):Connect(function()
			setspeed(plr, stunned)
		end)
	end)
end)

If it’s not, it’s probably best to leave it as is

I have merged the script into the stun system now and I tested it a few times, sometimes it would work and sometimes it wouldn’t and the player would be able to walk while stunned. I’m unsure of whether if my stun system is causing this or that it’s something else. (Sorry Kaiden I had to remove some things or else the script wouldn’t work.) As of now here is my script:

local debounce = false

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		local function setspeed(plr)
			while debounce == false do
				wait()
				if humanoid.Health >= 75 then
					humanoid.WalkSpeed = 30
				elseif humanoid.Health < 75 and humanoid.Health >= 50 then
					humanoid.WalkSpeed = 18
				elseif humanoid.Health < 50 then
					humanoid.WalkSpeed = 5
				end
			end
		end

		local newThread = coroutine.create(function()
			if debounce == false then
				setspeed()
			end
		end)
		coroutine.resume(newThread)

		local oldjumpPower = humanoid.JumpPower

		humanoid.ChildAdded:Connect(function(child)
			if child.Name == "StunEffect" then

				game.ReplicatedStorage.StunStart:FireClient(player)
				debounce = true
				print("true")

				humanoid.WalkSpeed = 0
				humanoid.JumpPower = 0
			end
		end)

		humanoid.ChildRemoved:Connect(function(childremoved)
			if humanoid:FindFirstChild("StunEffect") then
				debounce = true
				print("true")

				humanoid.WalkSpeed = 0
				humanoid.JumpPower = 0			
			else		
				game.ReplicatedStorage.StunFinish:FireClient(player)
				debounce = false
				print("false")

				humanoid.JumpPower = oldjumpPower
				setspeed()
			end
		end)
	end)
end)

Sorry for the late reply, but remove the while loop from the set speed function, and have it look like this:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		
		local function setspeed()
			if humanoid:FindFirstChild("StunEffect") then return end
			
			if humanoid.Health >= 75 then
				humanoid.WalkSpeed = 30
			elseif humanoid.Health < 75 and humanoid.Health >= 50 then
				humanoid.WalkSpeed = 18
			elseif humanoid.Health < 50 then
				humanoid.WalkSpeed = 5
			end
		end
		
		setspeed()
		humanoid:GetPropertyChangedSignal("Health"):Connect(setspeed)
		
		local oldjumpPower = humanoid.JumpPower
		
		humanoid.ChildAdded:Connect(function(child)
			if child.Name == "StunEffect" then
				game.ReplicatedStorage.StunStart:FireClient(player)

				humanoid.WalkSpeed = 0
				humanoid.JumpPower = 0
			end
		end)
		
		humanoid.ChildRemoved:Connect(function(child)
			if child.Name == "StunEffect" and not humanoid:FindFirstChild("StunEffect") then	
				game.ReplicatedStorage.StunFinish:FireClient(player)

				humanoid.JumpPower = oldjumpPower
				setspeed()
			end
		end)

I also changed some things about some of the functions to make it work much better. The speed would sometimes not work as the script would be looping the changing of walkspeeds, which isn’t really needed if we connect it to a health changed connection.

Sorry for the late reply too lol, but I ended up making the script work earlier by changing some stuff and also adding setspeed() at the end. I will take note of your script, and you could suggest some edits if you like.

local debounce = false
local scs = game.ReplicatedStorage:WaitForChild("SpeedChangeStart")
local sce = game.ReplicatedStorage:WaitForChild("SpeedChangeEnd") 

scs.Event:Connect(function()
	debounce = true
end)

sce.Event:Connect(function()
	debounce = false
end)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		local function setspeed()
			while debounce == false do
				wait()
				if humanoid.Health >= 75 then
					humanoid.WalkSpeed = 30
				elseif humanoid.Health < 75 and humanoid.Health >= 50 then
					humanoid.WalkSpeed = 18
				elseif humanoid.Health < 50 then
					humanoid.WalkSpeed = 5
				end
			end
		end

		local newThread = coroutine.create(function()
			setspeed()
		end)
		coroutine.resume(newThread)
		
		local function stunned()
			while debounce == true do
				wait()
				humanoid.WalkSpeed = 0
				humanoid.JumpPower = 0
			end
		end

		local oldjumpPower = humanoid.JumpPower

		humanoid.ChildAdded:Connect(function(child)
			if child.Name == "StunEffect" then
				game.ReplicatedStorage.StunStart:FireClient(player)
				debounce = true
				print("true")

				stunned()
				
			end
		end)

		humanoid.ChildRemoved:Connect(function(childremoved)
			if humanoid:FindFirstChild("StunEffect") then
				debounce = true
				print("true")

				stunned()
				
			else	
				
				game.ReplicatedStorage.StunFinish:FireClient(player)
				debounce = false
				print("false")

				humanoid.JumpPower = oldjumpPower
				setspeed()
			end
		end)
	end)
end)

Ps: I found my system ineffective later on since it did not actually do the stun properly, along with creating a few other issues. Your script works perfectly and I have to thank you once again because you blew away a problem that I was stuck with for 50 hours.

1 Like