Trying to make a CTRL toggle sprint script, but it stops working after a new character is loaded in

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

A sprint script that checks the class’ maxSpeed attribute value and ensures that the character’s speed does not exceed that value.

  1. What is the issue? Include screenshots / videos if possible!

It does not run after a new character is loaded in. It prints the maxSpeed value on the client twice after their character is switched, but the sprinting itself only works on the default character that you spawn in as.

  1. What solutions have you tried so far? Did you look for solutions on the Creator Hub?

I have tried a ton of messing around with player.CharacterAdded, which often fixed one problem and created another, or just didn’t work altogether. I checked and asked for solutions on multiple roblox scripting servers and looked for similar issues here, also to no benefit.

Sprint (local script in StarterCharacterScripts)

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
--local clientHandler = require(game:GetService("StarterPlayer").ClientHandler)

local isRunning = false
local SPD = Character:GetAttribute("maxSpeed")

Player.CharacterAdded:Connect(function(char)
	
	SPD = char:GetAttribute("maxSpeed")
	print("maxSpeed: " ..SPD)
	
end)

UIS.InputBegan:Connect(function(input, gp)
	if gp then return end
		if input.KeyCode == Enum.KeyCode.LeftControl and Humanoid.MoveDirection.Magnitude > 0 then
			if isRunning == false then
				Humanoid.WalkSpeed = SPD

			elseif isRunning then   

				Humanoid.WalkSpeed = 16
			end
		end
	end)

ClassLoader In ServerScriptService:

SwitchEvent.OnServerEvent:Connect(function(player, camPos)
	print("Server")
	
	local pos = player.Character.PrimaryPart.CFrame

	ClassMod:random(player) --Randomizes class selection
	
	player:LoadCharacter() -Reloads the character
	
	ClassMod:Replace(player) -Replaces them with the character of the selected class
	
	
	player.Character.PrimaryPart.CFrame = pos
	--print(camPos)
	camPos = camPos
end)

I am also worried about the security of storing a character’s max speed in an attribute. Wouldn’t an exploiter be able to just change it via :setAttribute and run as fast as they want? I do plan to rescript sprint but I need to make sure this rough draft will work first.

2 Likes

im not good at attributes, i tried it before but the results were bugged. Attributes might be faster than values but its not noticable in, for example, fast paced fighting games. This reply is alternative suggestions:

  1. for security, check a value in replicated storage.
  2. if the maxspeed value is different for every player and its based on a simple level system, calculate it using the level number.
  3. variable inside script

I think it’s unnecessary to have the player.CharacterAdded event because it will run whenever the character gets reset.

I would remove the event entirely and see if it fixes the problem.

And also, you should set the variable “isRunning” when you’re running/not running.

if not isRunning then
Humanoid.WalkSpeed = SPD
else
Humanoid.WalkSpeed = 16
end

isRunning = not isRunning


-- make sure the script its inside StarterCharacterScripts
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")

local SPD = Character:GetAttribute("maxSpeed")

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and Humanoid.MoveDirection.Magnitude > 0  then
		Character.Humanoid.WalkSpeed = SPD
	end
end)


UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 16
	end
end)

It does work better now, but the previous issue of sprinting not working after the character is replaced is still there

Any errors on the dev-console or at least warnings?

None at all, which is strange, I thought there’d at least be something