Damage Script intervenes with Sprint Script causing speed to fluctuate

The title pretty much explains the issue. So, the rigs that I use for a game I am working on has 2 scripts. One script is a shift to sprint script and the other is a damage effect. After the damage effect is dealt, the speed begins to decrease and increase when pressing shift. I know what the issue is but I don’t know how to fix it. Basically in the damage script, it says that the speed is 10 if the health is above 30. And the shift to sprint script changes the speed from 10 to 30 when shift is clicked. Now the issue is that while shift is clicked it should be 30 but according to the damage script if the health is above 30 it should be 10, so it fluctuates. I also tried to make it so that you can’t use shift to sprint while the damage effect is happening but I failed a lot of times.

Here’s the shift to sprint script:

local player = game.Players.LocalPlayer
local blurEffect = game.Lighting.Blur
local colorCorrectionEffect = game.Lighting.ColorCorrection
local sound = workspace.Heartbeatsound
local isPlayerDead = false
local Image = player.PlayerGui.Damage2.ImageLabel
local Sprint = game.StarterGui.ReadMe

local function updateEffects()
	if isPlayerDead then return end
	if player.Character.Humanoid.Health < 30 then
		blurEffect.Size = 10
		colorCorrectionEffect.Brightness = -0.1
		colorCorrectionEffect.TintColor = Color3.new(1, 0.537255, 0.537255)
		sound:Play()
		player.Character.Humanoid.WalkSpeed = 3
		Image.Visible = true
		Sprint.Disabled = true
	else
		blurEffect.Size = 0
		colorCorrectionEffect.TintColor = Color3.new(1, 1, 1)
		sound:Stop()
		player.Character.Humanoid.WalkSpeed = 10
		Image.Visible = false
		Sprint.Disabled = false
	end
end

player.CharacterAdded:Connect(function(character)
	blurEffect.Size = 0
	colorCorrectionEffect.Brightness = 0
	colorCorrectionEffect.TintColor = Color3.new(1, 1, 1)
	sound:Stop()
	player.Character.Humanoid.WalkSpeed = 10
	Image.Visible = false
	Sprint.Disabled = false
end)


updateEffects()

player.Character.Humanoid.HealthChanged:Connect(updateEffects)

Here is a shift to sprint script:

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character


UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
 if input.KeyCode == Enum.KeyCode.LeftControl then
  Character.Humanoid.WalkSpeed = 30
  local Anim = Instance.new('Animation')
		Anim.AnimationId = 'Id'
  PlayAnim = Character.Humanoid:LoadAnimation(Anim)
  PlayAnim:Play()
 end
end)


UIS.InputEnded:connect(function(input)
 if input.KeyCode == Enum.KeyCode.LeftControl then
  Character.Humanoid.WalkSpeed = 10
  PlayAnim:Stop()
 end
end)
3 Likes

Use this same type of code in your Sprint Script:

if player.Character.Humanoid.Health < 30 then

You are not checking what the health is before applying the Character.Humanoid.WalkSpeed = 30

2 Likes

Wait so what exactly do I do? (filling in the character thingy don’t mind this)

2 Likes

You ran a check to see what the health was in one script:

local function updateEffects()
	if isPlayerDead then return end
	if player.Character.Humanoid.Health < 30 then

But you did not do the same in the other.

The < 30 means less than 30.

So that script will not run the function if the player health is below 30.

But your Sprint Script will run no matter what.

3 Likes

Wait so do I put this:

local function updateEffects()
	if isPlayerDead then return end
	if player.Character.Humanoid.Health < 30 then

in shift to sprint?

2 Likes

But this isn’t the issue though. I think it’s because the damage script detected that the health is above 30 so it made the speed 10. And at the same time, when the shift button is clicked it turns the speed into 30 but the health is still above 30 so the damage script brings it back to 10 and so on.

1 Like

Try adding the line:

if player.Character.Humanoid.Health < 30 then return end

in these two place and see what happens:

if input.KeyCode == Enum.KeyCode.LeftControl then
	if player.Character.Humanoid.Health < 30 then return end
if input.KeyCode == Enum.KeyCode.LeftControl then
	if player.Character.Humanoid.Health < 30 then return end
1 Like

So like this?

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character


UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
	if input.KeyCode == Enum.KeyCode.LeftControl then
		if Player.Character.Humanoid.Health < 30 then return end
		Character.Humanoid.WalkSpeed = 30
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'Id'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
	end
end)


UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		if Player.Character.Humanoid.Health < 30 then return end
		Character.Humanoid.WalkSpeed = 10
		PlayAnim:Stop()
	end
end)
1 Like

Yes, sort of.

I was just saying to try it and see what happens.

I see a few problems with my suggestion, but it’s just a quick check to see if the flip flopping stops.

2 Likes

The sprint doesn’t work while damaging which is what I wanted but the speed fluctuating is still an issue.

This is what it looked like before (You can sprint while damaging and post-damage sprinting breaks) :

This is what it looks like after the line you told me to add (The sprinting stops during the damage effect but post-damage sprinting breaks) :

2 Likes

Okay.

Try removing the WalkSpeed from the UpdateEffects.

Just have it in the InputBegan and InputEnded.

Like so:

local player = game.Players.LocalPlayer
local blurEffect = game.Lighting.Blur
local colorCorrectionEffect = game.Lighting.ColorCorrection
local sound = workspace.Heartbeatsound
local isPlayerDead = false
local Image = player.PlayerGui.Damage2.ImageLabel
local Sprint = game.StarterGui.ReadMe

local function updateEffects()
	if isPlayerDead then return end
	if player.Character.Humanoid.Health < 30 then
		blurEffect.Size = 10
		colorCorrectionEffect.Brightness = -0.1
		colorCorrectionEffect.TintColor = Color3.new(1, 0.537255, 0.537255)
		sound:Play()
		--player.Character.Humanoid.WalkSpeed = 3
		Image.Visible = true
		Sprint.Disabled = true
	else
		blurEffect.Size = 0
		colorCorrectionEffect.TintColor = Color3.new(1, 1, 1)
		sound:Stop()
		--player.Character.Humanoid.WalkSpeed = 10
		Image.Visible = false
		Sprint.Disabled = false
	end
end

player.CharacterAdded:Connect(function(character)
	blurEffect.Size = 0
	colorCorrectionEffect.Brightness = 0
	colorCorrectionEffect.TintColor = Color3.new(1, 1, 1)
	sound:Stop()
	player.Character.Humanoid.WalkSpeed = 10
	Image.Visible = false
	Sprint.Disabled = false
end)


updateEffects()

player.Character.Humanoid.HealthChanged:Connect(updateEffects)



-- SPRINT SCRIPT


local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character


UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
	if input.KeyCode == Enum.KeyCode.LeftControl then
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'Id'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		-- make sure they are not damaged
		if player.Character.Humanoid.Health < 30 then
			player.Character.Humanoid.WalkSpeed = 3
		else
			Character.Humanoid.WalkSpeed = 30
		end
	end
end)


UIS.InputEnded:connect(function(input)		
	if input.KeyCode == Enum.KeyCode.LeftControl then
		PlayAnim:Stop()
		-- make sure they are not damaged
		if player.Character.Humanoid.Health < 30 then
			player.Character.Humanoid.WalkSpeed = 3
		else
			Character.Humanoid.WalkSpeed = 10
		end
	end
end)
1 Like

I put it in the same script and the sprinting does not work in the first place

local player = game.Players.LocalPlayer
local blurEffect = game.Lighting.Blur
local colorCorrectionEffect = game.Lighting.ColorCorrection
local sound = workspace.Heartbeatsound
local isPlayerDead = false
local Image = player.PlayerGui.Damage2.ImageLabel
local Sprint = game.StarterGui.ReadMe

local function updateEffects()
	if isPlayerDead then return end
	if player.Character.Humanoid.Health < 30 then
		blurEffect.Size = 10
		colorCorrectionEffect.Brightness = -0.1
		colorCorrectionEffect.TintColor = Color3.new(1, 0.537255, 0.537255)
		sound:Play()
		player.Character.Humanoid.WalkSpeed = 3
		Image.Visible = true
		Sprint.Disabled = true
	else
		blurEffect.Size = 0
		colorCorrectionEffect.TintColor = Color3.new(1, 1, 1)
		sound:Stop()
		player.Character.Humanoid.WalkSpeed = 10
		Image.Visible = false
		Sprint.Disabled = false
	end
end

player.CharacterAdded:Connect(function(character)
	blurEffect.Size = 0
	colorCorrectionEffect.Brightness = 0
	colorCorrectionEffect.TintColor = Color3.new(1, 1, 1)
	sound:Stop()
	player.Character.Humanoid.WalkSpeed = 10
	Image.Visible = false
	Sprint.Disabled = false
end)


updateEffects()

player.Character.Humanoid.HealthChanged:Connect(updateEffects)



-- SPRINT SCRIPT


local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character


UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
	if input.KeyCode == Enum.KeyCode.LeftControl then
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'Id'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		-- make sure they are not damaged
		if player.Character.Humanoid.Health < 30 then
			player.Character.Humanoid.WalkSpeed = 3
		else
			Character.Humanoid.WalkSpeed = 30
		end
	end
end)


UIS.InputEnded:connect(function(input)		
	if input.KeyCode == Enum.KeyCode.LeftControl then
		PlayAnim:Stop()
		-- make sure they are not damaged
		if player.Character.Humanoid.Health < 30 then
			player.Character.Humanoid.WalkSpeed = 3
		else
			Character.Humanoid.WalkSpeed = 10
		end
	end
end)
2 Likes

Make the sprint script not SET the walkspeed, instead of setting it to 30, simply make it add 20 to the existing walkspeed, and same with the dmg script, make it simply do a player.Character.Humanoid.Walkspeed -= 7 instead of a =

also store your humanoid in a variable pleaseeeee

3 Likes

I just pasted your scripts together for ease of reading.

The sprint script need to be a Local Script and placed in you StarterCharacterScripts.

I just tested it and it works as expected.

local UIS = game:GetService('UserInputService')
local player = game.Players.LocalPlayer
local Character = player.Character


UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
	if input.KeyCode == Enum.KeyCode.LeftControl then
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'Id'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		-- make sure they are not damaged
		if player.Character.Humanoid.Health < 30 then
			player.Character.Humanoid.WalkSpeed = 3
		else
			Character.Humanoid.WalkSpeed = 30
		end
	end
end)


UIS.InputEnded:connect(function(input)		
	if input.KeyCode == Enum.KeyCode.LeftControl then
		PlayAnim:Stop()
		-- make sure they are not damaged
		if player.Character.Humanoid.Health < 30 then
			player.Character.Humanoid.WalkSpeed = 3
		else
			Character.Humanoid.WalkSpeed = 10
		end
	end
end)
1 Like

Oh I misread the wanted result.

instead of setting the walkspeed to 3 if the health is below 30 on the spriting function (which plays the new animation and everything), you could instead do

UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
	if input.KeyCode == Enum.KeyCode.LeftControl then
		if player.Character.Humanoid.Health < 30 then return end
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'Id'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		-- make sure they are not damaged
		Character.Humanoid.WalkSpeed = 30
	end
end)

now, if you DO want the animation to play even if you are damaged (while "sprinting), then use the other answer instead

2 Likes

Oh nevermind I FEEL SO DARN STUPID

reread OP again, if your wanted result is instead to make sprinting still work after taking dmg, then the issue is actually that the player is healing, which activates the HealthChanged effect. You could either disable player healing, or check to see if the health change was positive or negative

oh yea and youd also do the whole “if health < 30 then walkspeed = 3” for the sprint part for when you release shift

1 Like

Fluctuation happens because normal characters spawns with a Health script that makes you regenerate Health over time. Every second or so, the health changes a little because of the regen, making your HealthChanged fire and set the speed to 3 again.

To solve that, you first have to make the script runs only when the player is damaged. You can know that by saving the last health value and comparing to the new one, if the difference is negative, then you are damaged, not healed.

local lastHealth = Humanoid.Health
Humanoid.HealthChanged:Connect(function(health)
    if health < lastHealth then
        -- damaged
    end
    lastHealth = health -- save for the next change
end)

second, you might want to check if the player is pressing shift when is time to set the slow from the effect back, like:

local UIS = game:GetService("UserInputService")

-- your updateEffects function would be like
local function updateEffects()
if isPlayerDead then return end
if player.Character.Humanoid.Health < 30 then
	blurEffect.Size = 10
	colorCorrectionEffect.Brightness = -0.1
	colorCorrectionEffect.TintColor = Color3.new(1, 0.537255, 0.537255)
	sound:Play()
	player.Character.Humanoid.WalkSpeed = 3
	Image.Visible = true
	Sprint.Disabled = true
else
	blurEffect.Size = 0
	colorCorrectionEffect.TintColor = Color3.new(1, 1, 1)
	sound:Stop()
	player.Character.Humanoid.WalkSpeed = UIS:IsKeyDown(Enum.KeyCode.LeftControl) and 30 or 10
	Image.Visible = false
	Sprint.Disabled = false
end
3 Likes

Which script do I put this one inside?

1 Like

updateEffects() is the function handling the damage, so you can put the call inside there.

local lastHealth = Humanoid.Health
Humanoid.HealthChanged:Connect(function(health)
    if health < lastHealth then
        updateEffects()
    end
    lastHealth = health -- save for the next change
end)

sinse you does not have a variable called Humanoid, you can use

player.Character.Humanoid.HealthChanged:Connect(...

instead, or create that variable like this:

local Humanoid = player.Character.Humanoid.Health
2 Likes

So should the damage script look like that?

local player = game.Players.LocalPlayer
local blurEffect = game.Lighting.Blur
local colorCorrectionEffect = game.Lighting.ColorCorrection
local sound = workspace.Heartbeatsound
local isPlayerDead = false
local Image = player.PlayerGui.Damage2.ImageLabel
local Sprint = game.StarterGui.ReadMe
local UIS = game:GetService("UserInputService")
local Humanoid = player.Character.Humanoid.Health

-- your updateEffectsFunction would be like
local function updateEffects()
	if isPlayerDead then return end
	if player.Character.Humanoid.Health < 30 then
		blurEffect.Size = 10
		colorCorrectionEffect.Brightness = -0.1
		colorCorrectionEffect.TintColor = Color3.new(1, 0.537255, 0.537255)
		sound:Play()
		player.Character.Humanoid.WalkSpeed = 3
		Image.Visible = true
		Sprint.Disabled = true
	else
		blurEffect.Size = 0
		colorCorrectionEffect.TintColor = Color3.new(1, 1, 1)
		sound:Stop()
		player.Character.Humanoid.WalkSpeed = UIS:IsKeyDown(Enum.KeyCode.LeftControl) and 30 or 10
		Image.Visible = false
		Sprint.Disabled = false
	end
end

local lastHealth = Humanoid.Health
Humanoid.HealthChanged:Connect(function(health)
	if health < lastHealth then
		updateEffects()
	end
	lastHealth = health -- save for the next change
end)
1 Like