My shift to sprint script bugging

To be honest this was my first time publishing a model or script to Roblox but after one day when I tried my code, it was working fine but when I die and respawn, it doesn’t work for some reason unless I restart the game.
Here is the code.

--Variables
local cam = workspace.CurrentCamera
local tweenservice = game:GetService("TweenService")
local sprintspeed = 120--change the speed to how much ever you want
local normalspeed = 16
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local UIS = game:GetService("UserInputService")
local humanoid = char:WaitForChild("Humanoid")

--Fov
local info = TweenInfo.new(0.1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local fovgoal = {FieldOfView = 80}
local retinfo = TweenInfo.new(0.1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local retfovgoal = {FieldOfView = 70}
local tw = tweenservice:Create(cam,info,fovgoal)
local rettw = tweenservice:Create(cam,retinfo,retfovgoal)
--Input
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if humanoid.MoveDirection.Magnitude>0 then
			tw:Play()
			humanoid.WalkSpeed = sprintspeed
		end
	end
end)
UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		rettw:Play()
		humanoid.WalkSpeed = normalspeed
	end
end)

A help would be helpful

Put this script inside StarterCharacterScripts, not inside StarterPlayerScripts.

Or make use of the LocalPlayer.CharacterAdded connection, which will be a drag.

1 Like

Where is located this local script?
If its inside CharacterScripts, then, it will be deleted once the character is destroyed.

Its the opposite isnt?

The player scripts always stays in Player instance, character ones are inside the model, and it will be destroyed upon dead

Starterplayerscrupts I though local scripts only work in both of this folders

Yup, local scripts works in player scripts, character scripts, player gui/starter gui, starter pack as well
Just do what @KJry_s said. In player the script always stay but lose its connection cause the body parts were deleted, if you put it in character scripts, it will be destroyed when player dies, and given again when player respawn.

You could check CharacterAdded event as well, as Realism said too.

Just read what I said, what happens is the script only runs when the Player loads, so when the character dies the character disappears from the variable and becomes nil, same for the Humanoid, to fix this you either have to constantly update the character via player.CharacterAdded or just be smart and put it inside StarterCharacterScripts.

Ok can you give me the fixed code with characateradded event ?

Is this in starterplayer? if so then it’ll only run once, use it on startercharacter it’s better.

Okk so I will try doing that. But can anyone explain the difference between the starter player and startercharacter folder .
Why do you think it is better in starter character?

StarterPlayer runs almost once when player joins
StarterCharacters runs again whenever player respawns.

As I said, StarterCharacterScript is better than StarterPlayerScript, because each time the Character respawn a new script gets added if you put it inside CharacterScript and each time we have to update it, because you’re not manually updating it yourself.

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