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)
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.
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)
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)
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)
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 =
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)
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
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
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
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
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)