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

Implementing sprinting in my game, but it wont work?

I have developed a localscript in StarterPlayerScripts and a script in ServerScriptService.

The purpose of these scrips is to add sprinting when the player presses LeftControl.

Here is my code. It is not too many lines so do not worry.

StarterPlayerScripts LocalScript

local uis = game:GetService("UserInputService")

local toggleSprintEvent = game.ReplicatedStorage:WaitForChild("ToggleSprint")

uis.InputBegan:Connect(function(key, processed)
	if processed then return end
	
	if key.KeyCode == Enum.KeyCode.LeftControl then
		
		toggleSprintEvent:FireServer(true)
	end
end)


uis.InputEnded:Connect(function(key, processed)
	if processed then return end
	
	if key.KeyCode == Enum.KeyCode.LeftControl then
		
		toggleSprintEvent:FireServer(false)
		
	end
end)

SeverScriptService Script

local toggleSprintEvent = game.ReplicatedStorage:WaitForChild("ToggleSprint")

toggleSprintEvent.OnServerEvent:Connect(function(player, turnOnSprrint)
	local char = player.Character
	
	if not char:FindFirstChild("Humanoid") then return end
	
	if turnOnSprrint == true then
		char:FindFirstChild("Humanoid").WalkSpeed = 50
		
	else
		char:FindFirstChild("Humanoid").WalkSpeed = 16
	end
end)

Please help!!

4 comments

0

I mean Left Control as in the key. Thanks for asking so that i can clarify for anyone else who helps!

0

I tested your code and it works perfectly fine for me. Check if you put in a remote event or remote function. You should be using a remote event under the replicated storage.

0

Haha wow I didnt even noticed I forgot that man. Thank you so much for reminding, I was going crazy.

0

Np, brother!

1 answer

✓ Accepted answer

Greetings.

Firstly I recommend when you're fetching things from ReplicatedStorage, use 'FindFirstChild() 'instead. 'WaitForChild()' is used on the client when things are loading, or if your game relies on streaming enabled. Either one of those will not affect ReplicatedStorage though so you don't need to use that.

Secondly, your code looks 100% fine. Something you might learn later, but is worth telling you now is that this is 100% unneeded and will only delay sprint. I personally don't see it as a bad practice if you're going to keep it local.

In other words, if you remove the remote event and adjust the players speed on the client, IT WILL WORK. (Unless you plan on adjusting how your characters network ownership is but that is pretty unlikely)

Due to how player network ownership works, it means anything you do with your character (mostly) will replicate to the server. You can teleport your player anywhere you'd want with a local script, and it would replicate to the server due to how network ownership is. The same applies for walkspeed. You can up your humanoids walkspeed on the client, saving you remote event delay.

I hope that makes sense.

2 comments

0

I was also thinking that any keybind event could just be written local instead of a remote event. However, I don't know how to do that. Would you mind giving an example? I'm sorry, I just really wanna master RobloxLUA

0

There is not such thing as mastering it. The best programmers, in reality, just know a lot of stuff and know how to search the internet better than others. Anyway, to do it locally, quite literally just do game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 50 (or 16 when you turn sprint off). Then just put that line in as a replacement for when the remote event gets fired.

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