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

Shift to Sprint script not working, need assistance; If possible?

Made this awhile back and want to get started at making my own game. Idky my sprinting script aint working, but the gui is working which is great. If you can help, it would be greatly appreciated.

local CAS = game:GetService("ContextActionService")
local RE = game:GetService("ReplicatedStorage"):WaitForChild("RE")
local fill = script.Parent:WaitForChild("top"):WaitForChild("fill")

local stam = 50
local speed = 16
local sprinting = false

local function sprint (str,state,object)
	if stam <1 then return end
	speed = state == Enum.UserInputState.Begin and 24 or 16
	sprinting = state == Enum.UserInputState.Begin
	while sprinting and stam > 0 and wait(.1) do
		stam = stam - 1
		fill:TweenSize(UDim2.new(stam/50, 0, 1, 0),"Out","Quint", .1, true)  
		RE:FireServer('sprint', (speed))
	end
 	sprinting = false
	speed = 16
	RE:FireServer('sprint', (speed))
	wait(1)
	while not sprinting and stam < 50 and wait (.5) do
		fill:TweenSize(UDim2.new(stam/50, 0, 1, 0), "Out", "Quint", .1, true)
		stam = stam +0.5
	end
end

CAS:BindAction("sprint", sprint, true, Enum.KeyCode.LeftShift)

--sprinting does not work, but the gui is currently working when you hold down, and it also regens back.

2 comments

0

I feel like its the lines 11 and 12 that is making it not work, but I don't know. Any errors?

0

no errors, code went through well, just that when I press shift, im not running at all.

2 answers

✓ Accepted answer

I have no clue what the remote event is connected to, so I'll answer based on all the code posted here: You forgot to set the WalkSpeed property of the localplayer's humanoid.

Oh and, you are also carelessly passing through the speed value, which could very easily be exploited. Instead, try sending all the keys pressed over to the server, and have the server handle stuff such as sprinting.

3 comments

0

lol youre talking way too smart, can you help me out further? I am confused. How do I send all key press to server?

0

Easy, set up a pressed keys table, and whenever a key gets pressed, add the keycode of that key into the table with a value of true, and if the key gets released, set the value to nil. Then, each time a key gets pressed or released, fire a remote event with the keys table, and set up a server script that copies the list into a pressedkeys table, with the key being the player's userid

0

If you message me on roblox, I'll link you a demo that has this in place.

hey man, i dont know to much of really advanced scripting but i recently made a sprint script with a gui as well and ill link that to you here to look through:

https://www.roblox.com/library/3455933485/SprintGUI

alternatively here is the script for it:

--variables
local textlabel = script.Parent
local player = game.Players.localPlayer
local stamina = 100
local uis = game:GetService("UserInputService")
local isSprinting = false



uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift and stamina >0 then
		isSprinting = true
		player.Character.Humanoid.WalkSpeed = 25 
		while key.KeyCode == Enum.KeyCode.LeftShift and stamina >0 and isSprinting == true do
			stamina = stamina -1
			print(stamina)
			wait(0.1)
			if stamina <= 0 then
				isSprinting = false
				player.Character.Humanoid.WalkSpeed = 16
			end
		end
		wait(0.1)
	end
end)





	uis.InputEnded:Connect(function(key)
		if key.KeyCode == Enum.KeyCode.LeftShift then
			isSprinting = false
		
		player.Character.Humanoid.WalkSpeed = 16
		wait(2)
			while key.KeyCode == Enum.KeyCode.LeftShift and stamina <100 and isSprinting == false do
			stamina = stamina +1 
			wait(0.1)
			print(stamina)
			end
		end
	end)
	while wait (0.1) do
		textlabel.Text = ("Sprint: "..stamina)
	end

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