I created a simple ShiftToRun LocalScript (pasted below).
It works fine, but after death + respawn it stops working, if I put it in: StarterPlayerScripts, or ReplicatedFirst.
However, it keeps working after death, if I put it in StarterGui.
I have a few questions (if I may?):
- I could not find any concrete info on this, but it appears that the
LocalScriptis destroyed (and thus the callback is removed fromContextActionService) on death for some reason. Is that about right? - More importantly, is there a clear guideline on how to persist
LocalScriptsafter death? IsStarterGuithe only way to persist for a script after death? - And, in general, when to put what kind of script where? What are the pros and cons of all the different locations? Is there any general guideline or a tabular overview somewhere?
- What about general overview over script lifecycles? When and how long do they live? What happens when they get destroyed?
local player = game.Players.LocalPlayer
local cas = game:GetService("ContextActionService")
local runSpeed = script.RunSpeed.Value
local isRunning = false
local char = player.Character or player.CharacterAdded:wait()
local humanoid = char:WaitForChild('Humanoid')
local walkSpeed = humanoid.WalkSpeed
local toggleRun = function(actionName, actionInputState, actionInputObject)
--if actionInputState == Enum.UserInputState.Begin then
isRunning = not isRunning
if isRunning then
humanoid.WalkSpeed = runSpeed
else
humanoid.WalkSpeed = walkSpeed
end
--end
end
cas:BindAction("toggleRun", toggleRun, true, Enum.KeyCode.LeftShift)

1 comment
You can put it in StarterPlayerScripts.