Script does not slow player down with health is lost?

Hello, So I’m making a local script that is supposed to slow the player down if damage is take. Basically, the method is to check every frame the player’s health, and if it’s less than 70, then they are supposed to slow the player down. Here is my code:

-- Services
local RunService = game:GetService("RunService")

-- Variables
local Player = script.Parent
local Humanoid = Player.Humanoid
local Health = Humanoid.Health
local Speed = Humanoid.WalkSpeed

-- Tables
local HealthTable = {
	100 == 0,
	85 == 3.5,
	70 == 5,
	55 == 7,
	25 == 10,
	10 == 15,
}

RunService.RenderStepped:Connect(function()
	for i, v in pairs(HealthTable) do
		if i <= Health and i < 70 then
			Speed -= v
		end 
	end
end)

And it prints out an error of:
LocalScript:23: attempt to perform arithmetic (sub) on number and boolean

How can I fix my problem??

1 Like

Try this instead

-- Services
local RunService = game:GetService("RunService")

-- Variables
local Player = script.Parent
local Humanoid = Player.Humanoid

-- Tables
local HealthTable = {
	[10] = 15,
	[25] = 10,
	[55] = 7,
	[70] = 5, 
	[85] = 3.5, 
	[100] = 0,
}

RunService.RenderStepped:Connect(function()
	for i, v in pairs(HealthTable) do
		if i <= Humanoid.Health and i < 70 then
			Humanoid.WalkSpeed -= v
		end 
	end
end)

I set Humanoid Health to 55 and it set health to 0. At least it does set speed to SOMETHING.

1 Like

I found the problem, It automatically sets the speed to 0.

it sets it to 0 since it keeps removing, i’ll fix that for you rq

here this is a better code.

-- Services
local RunService = game:GetService("RunService")

-- Variables
local Player = script.Parent
local Humanoid = Player.Humanoid

-- Tables
local HealthTable = {
	[10] = 15,
	[25] = 10,
	[55] = 7,
	[70] = 5, 
	[85] = 3.5, 
	[100] = 0,
}

Humanoid.HealthChanged:Connect(function()
    if HealthTable[Humanoid.Health] then
        Humanoid.WalkSpeed -= HealthTable[Humanoid.Health]
    end
end)
2 Likes

Question, if the speed is not exactly 10 or 25, etc. it doesn’t set it. Advice?

here this is a better code.

-- Services
local RunService = game:GetService("RunService")

-- Variables
local Player = script.Parent
local Humanoid = Player.Humanoid

-- Tables
local HealthTable = {
	[10] = 15,
	[25] = 10,
	[55] = 7,
	[70] = 5, 
	[85] = 3.5, 
	[100] = 0,
}

Humanoid.HealthChanged:Connect(function()
	
	if HealthTable[Humanoid.Health] then
		Humanoid.WalkSpeed -= HealthTable[Humanoid.Health]
		return
	end
	
	for i , v in pairs(HealthTable) do
		if Humanoid.Health > HealthTable[i] and Humanoid.Health < HealthTable[i + 1] then
			Humanoid.WalkSpeed -= HealthTable[i] --or i + 1, your choice here
		end
	end
	
end)