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

Script Works In Studio But Not Actual Game?

I made a script that makes you sprint, it doesn't work well but it is good enough. The only problem is that it only works in the studio. It is in the starter Gui

Script is there

p = game.Players.LocalPlayer
char = Player.Character
Torso = Char.Torso
Mouse = Player:GetMouse()
 
function toSprint(key)
 key = key:lower()
   if key == "v" then
   char.Humanoid.WalkSpeed = 50
   else 
   char.Humanoid.WalkSpeed = 16
   end
end

Mouse.KeyDown:connect(toSprint)

I have no idea why it doesn't work? I am just learning how to do this so if someone could tell me why it doesn't work that would be great.

1 answer

Normally I'd just insert a script inside the character itself, because I'm used to it. But here's a set of code you could try. I've tested it and it works in the server. As long as this is in a LocalScript, it should execute fine.

p = game.Players.LocalPlayer	-- I've set 'p' as the player.

-- There are other ways to wait for the player. I used "repeat ... until [condition]" because I'm used to it.
repeat
	wait()		-- This keeps waiting until the instant that 'p' is fully rendered.
until p

Mouse = p:GetMouse()

ToggleSprint = false	-- Note this static variable.

function toSprint(key)
	key = key:lower()
	local char = p.Character
	if key == "v" then
		if ToggleSprint == false then		-- This is saying "If ToggleSprint is false, then make it true and have the WalkSpeed go 50. Otherwise, revert back to default speed."
			ToggleSprint = true
			char.Humanoid.WalkSpeed = 50
		else								-- Kind of like a light-switch effect. You can turn it on or off by the click of the 'v' button.
			ToggleSprint = false
			char.Humanoid.WalkSpeed = 16
		end
	end
end

Mouse.KeyDown:connect(toSprint)

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