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)
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.
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?
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.