Help with Sprint Script

Hello

I am trying to make a sprinting system, it works but I wanted to add a trail that the player leaves behind when they sprint, to indicate they they are sprinting.

I tried to do this but whenever I try it I always have this error in the Output:

SpeedTrail is not a valid member of ServerStorage "ServerStorage"

Here is the script I made:

local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Camera = game.Workspace.CurrentCamera

local Trail = game.ServerStorage.SpeedTrail:Clone()

local attachment0 = Instance.new("Attachment")
attachment0.Position = Vector3.new(0, 0.8, 0)
attachment0.Parent = Character.Torso

local attachment1 = Instance.new("Attachment")
attachment1.Position = Vector3.new(0, -1, 0)
attachment1.Parent = Character.Torso

Trail.Attachment0 = attachment0
Trail.Attachment1 = attachment1

local Sprinting = false

local function Sprint(ActionName, InputState)
if InputState == Enum.UserInputState.Begin then
Sprinting = true
Humanoid.WalkSpeed = 16*2
TweenService:Create(Camera, TweenInfo.new(1), {FieldOfView = 80}):Play()
Trail.Enabled = true

else

Sprinting = false
Humanoid.WalkSpeed = 16
TweenService:Create(Camera, TweenInfo.new(2), {FieldOfView = 70}):Play()
Trail.Enabled = false
      end
end

ContextActionService:BindAction("Sprint", Sprint, true, Enum.KeyCode.LeftShift, Enum.KeyCode.ButtonL2)

ContextActionService:SetImage("Sprint", "rbxassetid://1921587812")
ContextActionService:SetDescription("Sprint", "Run")
ContextActionService:SetPosition("Sprint", UDim2.new(.75, -50, 0, 1))
ContextActionService:GetButton("Sprint").Size = UDim2.new(.25, 0, .25, 0)

How can I do this?

local Trail
if game:GetService('ServerStorage'):FindFirstChild('SpeedTrail', true) then
     Trail = game:GetService('ServerStorage'):FindFirstChild('SpeedTrail', true):Clone()
else
    Trail = nil --// Or create a new instance of a trail to ensure trail isn't nil
end

You can not access ServerStorage in the client, use a client to server with a remote.

Or you can drag the trail to ReplicatedStorage, and change the parent. (Which is actually much preferred, instead of exploitable events)

2 Likes

Try changing
local Trail = game.ServerStorage.SpeedTrail:Clone()
to
local Trail = game.ServerStorage:WaitForChild("SpeedTrail"):Clone()

Edit: I did not notice this was from a local script. Try moving the speed trail to another location

I get an error saying:

Workspace.houston829.Sprint:24: attempt to index nil with 'Attachment0’

Is correct, you need to move the trail to a container that replicates to the client, preferably the ‘ReplicatedStorage’ folder as that’s its purpose.