Strange CAS Behavior

So I’ve got a local script in StarterCharacterScripts, and it’s binded using CAS. When you first spawn in, the print statement in it DOES print and prints CORRECTLY, yet there is no AlignPosition in the explorer, nor does the player get moved. After resetting your character, the script works as intended.

How do I make it work on the initial spawn?

local function movementHandler(actionName, inputState, inputObject)
	local tempData = httpService:JSONDecode(data.Value)
	
	if actionName == "Dash" and inputState == Enum.UserInputState.Begin and dashCooldown then
		dashCooldown = false
		
		local dashDistance = 20
		for _, skill in pairs(tempData["Skills"]) do
			dashDistance += skills[skill]["DashDistance"]["Additive"]
		end
		for _, skill in pairs(tempData["Skills"]) do
			dashDistance *= skills[skill]["DashDistance"]["Multiplicative"]
		end
		
		character["Left Arm"].Trail.Enabled, character["Right Arm"].Trail.Enabled = true, true
		
		local alignPosition = script.AlignPosition:Clone()
		alignPosition.MaxForce, alignPosition.Attachment0, alignPosition.Parent, alignPosition.Position = dashDistance * 1000, humanoidRootPart.AlignPositionAttachment, humanoidRootPart, (humanoidRootPart.CFrame * CFrame.new(0, 0, -dashDistance)).Position
		
		print(alignPosition.Parent.Parent, alignPosition.Parent)
		
		task.wait(0.2)
		
		alignPosition:Destroy()
		
		character["Left Arm"].Trail.Enabled, character["Right Arm"].Trail.Enabled = false, false
		
		task.wait(1.8)
		
		dashCooldown = true
	end
end

contextActionService:BindAction("Dash", movementHandler, true, Enum.KeyCode.Q, Enum.KeyCode.ButtonL3)

Before you answer:
YES, it is a local script
YES, it is in StarterCharacterScripts
YES, all variables are correct

So it’s in everything:

LocalScript = :white_check_mark:
StarterCharacterScripts = :white_check_mark:
Variables are correct = :white_check_mark:

Right ?

But i wonder if it works…

Yes, and even the print statement in it prints correctly on initial spawn, but nothing shows up in the explorer

You don’t set dashCooldown variable to true outside of the function so when it runs it’ll always be nil (false) and won’t get past the if statement

The only place where you set it to true is inside the function but because it never runs it’ll never be defined

If you did not read my post, I said all variables are correct and already explained that it runs correctly after resetting, I just left the global variables out as there are no issues with them and it is assumed they’re there.

A quick update: This issue sometimes doesn’t exist at all, or can be a spotty occurrence depending on the device.

My Desktop PC seems to have this issue on 100% of tests.

My laptop never appeared to have this issue.

And on console (Xbox One) it sometimes occured and sometimes did not.

Not sure what is going on, not enough information.

Personally I would attempt to try alternatives such as all the things listed below

  1. Increasing CAS priority,

https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindActionAtPriority

  1. Using starter player scripts instead.
  2. Using wait for child, maybe replication load order issue?
local alignPosition = script:WaitForChild("AlignPosition"):Clone()
  1. checking the attachment 0 and attachment 1s as well of the align position (There is alignposition instance but somehow the attachments are missing?)

  2. Creating the align position from scratch via code instead of cloning it (For the above issue #4)

  3. adding a dirty wait(1) on the top of the script, (Something hasn’t loaded in yet?)

Is that all to the script?

These are just ideas.

I’m going to mark this as solved, it has seemingly fixed itself, my only guess is that it was either some temporary engine bug, or some other script in my game somehow caused it.