Boolean is not being set to false on server

I’m attempting to set up a sprint system, but when I modify the value on my client, my isSprinting variable doesn’t update on the server. When I try to print it, it always outputs True even when i stop sprinting.

Module Client

local Sprint = {}

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local playerSprint = ReplicatedStorage:WaitForChild("PlayerSprint")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local runSpeed = 30
local walkSpeed = 16

local camera = game.Workspace.CurrentCamera
local TweenService = game:GetService('TweenService')

local sprintAnimationId = "http://www.roblox.com/asset/?id=17113833834"
local sprintAnimTrack = nil
local sprintAnimationLoaded = false
local sprintManaCostRate = 5  -- Mana cost per 0.5 seconds while sprinting
local isSprinting = false


-- Load sprint animation
local function loadAnimations()
	local sprintAnimation = Instance.new("Animation")
	sprintAnimation.AnimationId = sprintAnimationId
	sprintAnimTrack = humanoid:LoadAnimation(sprintAnimation)
	sprintAnimTrack.Looped = true
	sprintAnimTrack.Priority = Enum.AnimationPriority.Action
	sprintAnimTrack:Stop() -- Ensure animation starts stopped
	sprintAnimationLoaded = true
end

local function shiftToSprint()
	if  isSprinting then
		return
	end
	isSprinting = true
	playerSprint:FireServer(humanoid, isSprinting)
	humanoid.WalkSpeed = runSpeed
	TweenService:Create(camera, TweenInfo.new(0.5), { FieldOfView = 80 }):Play()
	if sprintAnimTrack and sprintAnimationLoaded  then
		sprintAnimTrack:Play() -- Play the sprint animation
	end
end


-- Input handling for sprinting (using Left Control)
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, IS)
	if IS then
		return
	end

	if inputObject.KeyCode == Enum.KeyCode.LeftControl then
		shiftToSprint()
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(inputObject, IS)
	if IS then
		return
	end

	if inputObject.KeyCode == Enum.KeyCode.LeftControl then
		if  isSprinting then
			TweenService:Create(camera, TweenInfo.new(0.5), { FieldOfView = 70 }):Play()
			 isSprinting = false
			humanoid.WalkSpeed = walkSpeed
			if sprintAnimTrack then
				sprintAnimTrack:Stop() -- Stop the sprint animation
			end
		end
	end
end)

-- Initial animation loading
loadAnimations()


return Sprint

Server

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerSprintEvent = ReplicatedStorage:WaitForChild("PlayerSprint")

local manaCost = 5
local manaRecovery = 2
local sprintManaCostRate = 5 
local walkSpeed = 16  
local lastSprintTime = 0  


PlayerSprintEvent.OnServerEvent:Connect(function(player, humanoid, isSprinting)
	local manaValue = player:WaitForChild("Values"):WaitForChild("Mana").Value

	if isSprinting then
		if manaValue >= sprintManaCostRate then
			print(isSprinting)
			while isSprinting and manaValue >= sprintManaCostRate do
				manaValue = manaValue - sprintManaCostRate
				print("Mana:", manaValue)
				player.Values.Mana.Value = manaValue
				wait(0.5) -- Adjust delay as needed
			end
		end
	else
		print(isSprinting)
	end
end)

This would be because even if you change something on the client, it won’t affect the server. To fix this you could use remoteEvents to send a message to the server whenever the player starts or stops sprinting

i am using a remote event so how would i do that

Basically you would want to have something like this
Client:

local remoteEvent = game.ReplicatedStorage.RemoteEvent

-- do this whenever you want to toggle the sprint variable on the server
remoteEvent:FireServer()

Server:

local remoteEvent = game.ReplicatedStorage.RemoteEvent
local sprintVar = false

remoteEvent.OnServerEvent:Connect(function()
	sprintVar = not sprintVar
end)

Make sure the remote event is in ReplicatedStorage so it can be accessed by both the server and the client.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.