LocalScript not working after being put in Backpack

When my LocalScript is inside Bakcpack before runtime, everything works. When I used a Script located in one of the GUI’s to move Local one (while runtime) to Backpack the Local Script only prints once (the print is in the first line of LocalScript). Here’s the Script used to move the Local one to Backpack:

script.Parent:WaitForChild("HandleFly").Disabled = false

script.Parent:WaitForChild("HandleFly").Parent = script.Parent.Parent.Parent:WaitForChild("Backpack")

And here’s the LocalScript:

-- Made by EgoMoose: https://devforum.roblox.com/t/how-to-make-a-character-fly/169309/5
-- Remixed by Jermartynojm: https://devforum.roblox.com/t/how-to-make-a-character-fly/169309/15
print("hello!") -- Only this prints
local camera = game:GetService("Workspace").CurrentCamera
print("after camera")
local character = game:GetService("Players").LocalPlayer.CharacterAdded:Wait()
print("yay")
local hrp = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local animate = character:WaitForChild("Animate")

while not character.Parent do character.AncestryChanged:Wait() end
local idleAnim = humanoid:LoadAnimation(script:WaitForChild("IdleAnim"))
local moveAnim = humanoid:LoadAnimation(script:WaitForChild("MoveAnim"))
local lastAnim = idleAnim

local bodyGyro = Instance.new("BodyGyro")
bodyGyro.maxTorque = Vector3.new(1, 1, 1) * 10 ^ 6
bodyGyro.P = 10 ^ 6;

local bodyVel = Instance.new("BodyVelocity")
bodyVel.maxForce = Vector3.new(1, 1, 1) * 10 ^ 6
bodyVel.P = 10 ^ 4

local isFlying, hasFlyPermissions = false, false
local movement = {forward = 0, backward = 0, right = 0, left = 0}

local function setFlying(flying)
	isFlying = flying
	bodyGyro.Parent = isFlying and hrp or nil
	bodyVel.Parent = isFlying and hrp or nil
	bodyGyro.CFrame = hrp.CFrame
	bodyVel.Velocity = Vector3.new()
	animate.Disabled = isFlying
	if isFlying then
		lastAnim = idleAnim
		lastAnim:Play()
	else
		lastAnim:Stop()
	end
end

local function onUpdate(dt)
	if isFlying then
		local cf = camera.CFrame
		local direction = cf.rightVector * (movement.right - movement.left) + cf.lookVector * (movement.forward - movement.backward)
		if direction:Dot(direction) > 0 then
			direction = direction.unit
		end
		bodyGyro.CFrame = cf
		bodyVel.Velocity = direction * humanoid.WalkSpeed * 3
	end
end

local function onKeyPress(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.F and not gameProcessedEvent and hasFlyPermissions then
		if not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead then
			return
		end
		for i, v in pairs(humanoid:GetPlayingAnimationTracks()) do
			v:Stop()
		end
		setFlying(not isFlying)
	end
end

local function movementBind(actionName, inputState, inputObject)
	if (inputState == Enum.UserInputState.Begin) then
		movement[actionName] = 1
	elseif (inputState == Enum.UserInputState.End) then
		movement[actionName] = 0
	end
	if isFlying then
		local isMoving = movement.right + movement.left + movement.forward + movement.backward > 0
		local nextAnim = isMoving and moveAnim or idleAnim
		if (nextAnim ~= lastAnim) then
			lastAnim:Stop()
			lastAnim = nextAnim
			lastAnim:Play()
		end
	end
	return Enum.ContextActionResult.Pass
end

game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("AdminGUI"):WaitForChild("MainRemote").OnClientEvent:Connect(function(EventType, Param1)
	if EventType == "Fly" then
		if Param1 == "Fly" then
			hasFlyPermissions = true
			setFlying(true)
		elseif Param1 == "Unfly" then
			hasFlyPermissions = false
			setFlying(false)
		end
	end
end)

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
game:GetService("ContextActionService"):BindAction("forward", movementBind, false, Enum.PlayerActions.CharacterForward)
game:GetService("ContextActionService"):BindAction("backward", movementBind, false, Enum.PlayerActions.CharacterBackward)
game:GetService("ContextActionService"):BindAction("left", movementBind, false, Enum.PlayerActions.CharacterLeft)
game:GetService("ContextActionService"):BindAction("right", movementBind, false, Enum.PlayerActions.CharacterRight)
game:GetService("RunService").RenderStepped:Connect(onUpdate)
1 Like

I said that it only prints ‘hello!’ and it works normally when put to StarterPack before runtime.

Just wondering, but where is HandleFly located before it is re-parented?

Also, character might not be the Player’s character, it seems to be only a reference to the CharacterAdded:Wait() function.

The line of code referencing the character might aswell be changed to something like this:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

Thanks, that was a solution:

I tried changing

to game:GetService("Players").LocalPlayer.Character but it did not work.

HandleFly is located in one of the GUIs.

Well, that’s because the character is not immediately avaliable, hence why Player.CharacterAdded:Wait() is needed along with it.