So I have a script that makes it so you sprint when you hold shift, it works in roblox studio testing area but sometimes when I reset in the actual published game I cant sprint when I respawn, any ideas on how to fix this problem?
local plr = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Camera = workspace.CurrentCamera
local RunButton = Enum.KeyCode.LeftShift -- Change this to the key you want for running
local newSpeed = 25 -- Speed you want while running
local DefaultFieldOfView = 70 -- Default FOV
local SprintFieldOfView = 85 -- Running FOV
local MobileButton = plr.PlayerGui.MobileGUI.MobileButtons.RunButton -- Location of the button
Camera.FieldOfView = DefaultFieldOfView
local function setupCharacter(character)
local Humanoid = character:WaitForChild("Humanoid")
local RunAnim = Humanoid:LoadAnimation(script:WaitForChild("Sprint"))
local JumpAnim = Humanoid:LoadAnimation(script:WaitForChild("Jump"))
local Running = false
local notMoving = true
local isOnGround = true
local Jumped = false
local originalSpeed = Humanoid.WalkSpeed
local db = true
local lastPosition = character.PrimaryPart.Position
local function Run()
if Running == false and isOnGround == true and script.RunEnabled.Value == true and Humanoid.WalkSpeed == originalSpeed then
Running = true
RunAnim:Play()
Humanoid.WalkSpeed = newSpeed
local goal1 = { FieldOfView = SprintFieldOfView }
local info1 = TweenInfo.new(0.5)
local tween1 = TweenService:Create(Camera, info1, goal1)
tween1:Play()
end
end
local function Stop()
if Running == true then
Running = false
RunAnim:Stop()
Humanoid.WalkSpeed = originalSpeed
local goal2 = { FieldOfView = DefaultFieldOfView }
local info2 = TweenInfo.new(1)
local tween2 = TweenService:Create(Camera, info2, goal2)
tween2:Play()
end
end
MobileButton.MouseButton1Click:Connect(function()
if Running == false then
Run()
elseif Running == true then
Stop()
end
end)
UIS.InputBegan:Connect(function(input, isTyping)
if isTyping then return end
if input.KeyCode == RunButton then
Run()
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == RunButton then
Stop()
end
end)
Humanoid.Changed:Connect(function(property)
if property == "MoveDirection" then
notMoving = Humanoid.MoveDirection.Magnitude == 0
if notMoving then
Stop()
end
end
if Running == true and (Humanoid:GetState() == Enum.HumanoidStateType.Physics or
Humanoid:GetState() == Enum.HumanoidStateType.Seated or
Humanoid:GetState() == Enum.HumanoidStateType.Swimming or
Humanoid:GetState() == Enum.HumanoidStateType.Climbing or
Humanoid:GetState() == Enum.HumanoidStateType.Dead) then
Stop()
end
if Running == true and Humanoid:GetState() == Enum.HumanoidStateType.Jumping then
Jumped = true
RunAnim:Stop()
JumpAnim:Play()
end
if Running == true and Jumped == true and Humanoid:GetState() == Enum.HumanoidStateType.Landed then
Jumped = false
RunAnim:Play()
end
if Running == true and script.RunEnabled.Value == false then
Stop()
end
isOnGround = Humanoid.FloorMaterial ~= Enum.Material.Air
if isOnGround and Running == true and db == true then
db = false
local dust = game.ReplicatedStorage.Fx.Dust:Clone()
dust.Position = character:WaitForChild("HumanoidRootPart").Position + Vector3.new(0, -2.5, 0)
dust.Parent = workspace.Fx
dust.Name = "RunDust"
dust.Attachment.Dust:Emit(1)
game.Debris:AddItem(dust, 2.5)
wait(0.15)
db = true
end
if Running == true then
for i = 1, math.random(1,2) do
local rp = Instance.new("Part", workspace.Fx)
rp.Anchored = true
rp.CanCollide = false
rp.Transparency = 0.8
rp.Name = "RunParticle"
rp.Material = Enum.Material.SmoothPlastic
rp.CanQuery = false
rp.Size = Vector3.new(0.05, 0.05, math.random(2.5, 3.5))
local colorRandom = math.random(1,3)
if colorRandom == 1 then
rp.Color = Color3.fromRGB(107, 107, 107)
elseif colorRandom == 2 then
rp.Color = Color3.fromRGB(175, 175, 175)
elseif colorRandom == 3 then
rp.Color = Color3.fromRGB(148, 148, 148)
end
local dirCFrame = CFrame.new(lastPosition, character.PrimaryPart.Position)
rp.CFrame = dirCFrame * CFrame.new(math.random(-25, 25)/10, math.random(-2.5, 2.5), math.random(-2, 2))
game.Debris:AddItem(rp, 0.75)
TweenService:Create(rp, TweenInfo.new(0.75), {Transparency = 1, Size = Vector3.new(0, 0, 0), CFrame = rp.CFrame * CFrame.new(0,0,math.random(2.5, 4))}):Play()
end
end
lastPosition = character.PrimaryPart.Position
end)
end
plr.CharacterAdded:Connect(setupCharacter)
-- Run setup for the initial character
if plr.Character then
setupCharacter(plr.Character)
end