Script breaks after death even on startercharacterscripts

Hi,

the title says it all. My local script that handles movement keeps breaking on death, how do I fix this?
it is on startercharacterscripts.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterPlayer = game:GetService("StarterPlayer")

-- variables
local Player = Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

-- requires
local Knit = require(ReplicatedStorage.Packages.Knit)
local ContextActionUtility = require(ReplicatedStorage.Source.Modules.ContextActionUtility)
local calculateRelativeDirection = require(ReplicatedStorage.Source.Utilities.calculateRelativeDirection)
-- movement requires
local Dash = require(script.Dash)
local Sprint = require(script.Sprint)

-- controller
local Movement = Knit.CreateController{
	Name = "Movement"
}

local BACKWARDS_SPEED = (StarterPlayer.CharacterWalkSpeed/2)
local STRAFE_SPEED = (StarterPlayer.CharacterWalkSpeed/1.4)
local FSTRAFE_SPEED = (StarterPlayer.CharacterWalkSpeed/1.07)

local SPEED = {
	["Standing"] = STRAFE_SPEED,
	["Backward"] = BACKWARDS_SPEED,
	["Forward"] = StarterPlayer.CharacterWalkSpeed,

	["BackwardRight"] = BACKWARDS_SPEED,
	["BackwardLeft"] = BACKWARDS_SPEED,

	["ForwardRight"] = FSTRAFE_SPEED,
	["ForwardLeft"] = FSTRAFE_SPEED,

	["Right"] = STRAFE_SPEED,
	["Left"] = STRAFE_SPEED
}

function onDirection()
	local States = Humanoid:GetAttributes()
	local Direction = calculateRelativeDirection(workspace.CurrentCamera, Humanoid)
	local Speed = SPEED[Direction]
	
	if not States["Sprinting"]then
		Humanoid.WalkSpeed = Speed
	end
	
	local splitDirection = (Direction:gsub("^%s", ""))

	if splitDirection:match("Forward") then
		Sprint.Bind(true)
	else
		Sprint.Bind(false)
	end
	
	if Direction ~= "Standing" or Direction ~= "Foward" then
		Dash.Bind(true)
	else
		Dash.Bind(false)
	end
	
end

--function sprintChanged(...)
--	print(Humanoid:GetAttribute("Sprinting"))
--end

-- Connections
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(onDirection)
--Humanoid:GetAttributeChangedSignal("Sprinting"):Connect(sprintChanged)
return Movement

Scripts in startercharacterscripts are inside of the player model. Dying will be deleting the script along with the character model and running it again when you respawn. While that necessarily shouldn’t make a issue happen make sure it isn’t.

1 Like

I put it first on replicatedstorage and the same thing happened, then I tried connecting a characteradded and making it reconnect the movedirection connection, and inside the characteradded I changed the character variable to Character = char (from function) or LocalPlayer.CharacterAdded:Wait().

That didn’t work so I put it inside of startercharacterscripts and the same thing just happens.

Some footage, I really need a solution because this impacts everything in my game.

As you can see, the dash button disappears and the run doesnt even work anymore, neither does the movement.

1 Like

StarterCharacterScripts. The script will spawn in the character, so you can reference it with script.Parent

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterPlayer = game:GetService("StarterPlayer")

-- variables
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

-- requires
local Knit = require(ReplicatedStorage.Packages.Knit)
local ContextActionUtility = require(ReplicatedStorage.Source.Modules.ContextActionUtility)
local calculateRelativeDirection = require(ReplicatedStorage.Source.Utilities.calculateRelativeDirection)
-- movement requires
local Dash = require(script.Dash)
local Sprint = require(script.Sprint)

-- controller
local Movement = Knit.CreateController{
	Name = "Movement"
}

local BACKWARDS_SPEED = (StarterPlayer.CharacterWalkSpeed/2)
local STRAFE_SPEED = (StarterPlayer.CharacterWalkSpeed/1.4)
local FSTRAFE_SPEED = (StarterPlayer.CharacterWalkSpeed/1.07)

local SPEED = {
	["Standing"] = STRAFE_SPEED,
	["Backward"] = BACKWARDS_SPEED,
	["Forward"] = StarterPlayer.CharacterWalkSpeed,

	["BackwardRight"] = BACKWARDS_SPEED,
	["BackwardLeft"] = BACKWARDS_SPEED,

	["ForwardRight"] = FSTRAFE_SPEED,
	["ForwardLeft"] = FSTRAFE_SPEED,

	["Right"] = STRAFE_SPEED,
	["Left"] = STRAFE_SPEED
}

function onDirection()
	local States = Humanoid:GetAttributes()
	local Direction = calculateRelativeDirection(workspace.CurrentCamera, Humanoid)
	local Speed = SPEED[Direction]
	
	if not States["Sprinting"]then
		Humanoid.WalkSpeed = Speed
	end
	
	local splitDirection = (Direction:gsub("^%s", ""))

	if splitDirection:match("Forward") then
		Sprint.Bind(true)
	else
		Sprint.Bind(false)
	end
	
	if Direction ~= "Standing" or Direction ~= "Forward" then
		Dash.Bind(true)
	else
		Dash.Bind(false)
	end
	
end

--function sprintChanged(...)
--	print(Humanoid:GetAttribute("Sprinting"))
--end

-- Connections
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(onDirection)
--Humanoid:GetAttributeChangedSignal("Sprinting"):Connect(sprintChanged)
return Movement
1 Like

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