Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I disable shift to sprint while a GUI button is activated?

I have a gui button that when pressed changes the local players walk/run/idle animations as well as change the walk speed to 6.5. How would i disable shift to sprint while the animation is toggled on? I tried disabling the Sprint script within the animation script but you can still press shift and it makes the animation play too fast and it looks weird. Animate script


local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild('Humanoid')
local char = player.Character

script.Parent.MouseButton1Click:connect(function()
		char.Humanoid.WalkSpeed = 6.5
		char.Animate.walk.WalkAnim.AnimationId = "rbxassetid://3670529036"
		char.Animate.run.RunAnim.AnimationId = "rbxassetid://3670529036"
		char.Animate.idle.IdleAnim.AnimationId = "rbxassetid://3670529036"
        end)

sprint

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

UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 35
	end
end)


UserInputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 18
	end
end)

1 answer

✓ Accepted answer

Personally, I would advise you make a BoolValue inside of the player as such

CanSprint = Instance.new("BoolValue", game.Players.LocalPlayer)
CanSprint.Name = "CanSprint"

You should probably do this inside your sprint script, and then make it so that if CanSprint is false, you don't change speed

Sprint Script

local Player = game.Players.LocalPlayer
local Character = Player.Character
local UserInputService = game:GetService("UserInputService")
 
local CanSprint = Instance.new("BoolValue", Player) -- because you have the local Player value
CanSprint.Name = "CanSprint"
CanSprint.Value = true -- Because there is no reason not to have it as true straight away

UserInputService.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.LeftShift and CanSprint.Value == true then
        Character.Humanoid.WalkSpeed = 35
    end
end)

UserInputService.InputEnded:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.LeftShift and CanSprint.Value == true then
	--You will notice I added the CanSprint there too, to avoid changing speed after releasing it       
       Character.Humanoid.WalkSpeed = 18
    end
end)

Other Script

local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild('Humanoid')
local char = player.Character
local CanSprint = player:WaiForChild("CanSprint")

script.Parent.MouseButton1Click:connect(function()
        CanSprint.Value = false
        char.Humanoid.WalkSpeed = 6.5
        char.Animate.walk.WalkAnim.AnimationId = "rbxassetid://3670529036"
        char.Animate.run.RunAnim.AnimationId = "rbxassetid://3670529036"
        char.Animate.idle.IdleAnim.AnimationId = "rbxassetid://3670529036"
end)

Of course you would have to toggle it back when you want to be able to run again

1 comment

0

thanks i just added a bool value parented to the sprint script instead of using instance.new and it works now

This discussion is preserved as it was. New answers, comments, and votes are closed.