Character Can't Move

So I made my own custom movement gui however when it is disabled, the character can’t move. I have no idea what the problem is and have tried multiple solutions, all leading to nothing.

Here is my script:

local gui = script.Parent
local playergui = gui.Parent
local player = playergui.Parent
local mouse = player:GetMouse()

local userInputService = game:GetService("UserInputService")

local frLeft = gui:WaitForChild("FrameLeft")
local frRight = gui:WaitForChild("FrameRight")
local ibForward = frLeft:WaitForChild("ImageButtonForward")
local ibForwardLeft = frLeft:WaitForChild("ImageButtonForwardLeft")
local ibForwardRight = frLeft:WaitForChild("ImageButtonForwardRight")
local ibBackward = frLeft:WaitForChild("ImageButtonBackward")
local ibBackwardLeft = frLeft:WaitForChild("ImageButtonBackwardLeft")
local ibBackwardRight = frLeft:WaitForChild("ImageButtonBackwardRight")
local ibLeft = frLeft:WaitForChild("ImageButtonLeft")
local ibRight = frLeft:WaitForChild("ImageButtonRight")
local ibJump = frRight:WaitForChild("ImageButtonJump")
local colorOn, colorOff = Color3.new(1, 1, 1), Color3.new(0, 0, 0)

local controls = {
	forward = false;
	forwardLeft = false;
	forwardRight = false;
	backward = false;
	backwardLeft = false;
	backwardRight = false;
	left = false;
	right = false;
	jump = false;
}

local buttons = {
	[ibForward] = "forward";
	[ibForwardLeft] = "forwardLeft";
	[ibForwardRight] = "forwardRight";
	[ibBackward] = "backward";
	[ibBackwardLeft] = "backwardLeft";
	[ibBackwardRight] = "backwardRight";
	[ibLeft] = "left";
	[ibRight] = "right";
	[ibJump] = "jump";
}

local currentCharacter
local currentHumanoid

local rs = game:GetService("RunService").RenderStepped
local function swait()
	rs:wait()
end

local function controlOn(button, controlName)
	controls[controlName] = true
	button.ImageColor3 = colorOn
end

local function controlOff(button, controlName)
	controls[controlName] = false
	button.ImageColor3 = colorOff
end

local function allControlsOff()
	for button, controlName in pairs(buttons) do
		controlOff(button, controlName)
	end
end

local function allControlsOn()
	for button, controlName in pairs(buttons) do
		controlOn(button, controlName)
	end
end

for button, controlName in pairs(buttons) do
	button.InputBegan:connect(function(input)
		--print("InputBegan[" .. input.UserInputType.Name .. "]  for " .. controlName)
		if input.UserInputType == Enum.UserInputType.Touch then
			if controlName ~= "jump" then 
				allControlsOff()
			end
			controlOn(button, controlName)
		elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
			controlOn(button, controlName)
		end
	end)
	button.InputEnded:connect(function (input)
		--print("InputEnded[" .. input.UserInputType.Name .. "]  for " .. controlName)
		if input.UserInputType == Enum.UserInputType.Touch then
			controlOff(button, controlName)
		elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
			controlOff(button, controlName)
			end
		end)
end

local function onCharacter(character)
	currentCharacter = character
	currentHumanoid = character:WaitForChild("Humanoid")
end

script.Parent.Toggle.Changed:Connect(function()
	if script.Parent.Toggle.Value == true then
if player.Character then onCharacter(player.Character) end
player.CharacterAdded:connect(function () onCharacter(player.Character) end)

if userInputService.TouchEnabled then
	
	frLeft.Visible = true
	frRight.Visible = true
	
	userInputService.ModalEnabled = true
	spawn(function()
		wait(1)
		userInputService.ModalEnabled = false
		userInputService.ModalEnabled = true
	end)
	
	while true do		
		ibForwardLeft.ImageTransparency = (controls.forward or controls.left or controls.forwardLeft) and .5 or 1
		ibForwardRight.ImageTransparency = (controls.forward or controls.right or controls.forwardRight) and .5 or 1
		ibBackwardLeft.ImageTransparency = (controls.backward or controls.left or controls.backwardLeft) and .5 or 1
		ibBackwardRight.ImageTransparency = (controls.backward or controls.right or controls.backwardRight) and .5 or 1
		if currentHumanoid then
			local dir = Vector3.new(
				((controls.left or controls.forwardLeft or controls.backwardLeft) and -1 or 0) + ((controls.right or controls.forwardRight or controls.backwardRight) and 1 or 0),
				0,
				((controls.forward or controls.forwardLeft or controls.forwardRight) and -1 or 0) + ((controls.backward or controls.backwardLeft or controls.backwardRight) and 1 or 0)
			)
			currentHumanoid:Move(dir, true)
			if controls.jump then
				currentHumanoid.Jump = true
			end
		end
		swait()
	end
else
	frLeft.Visible = true
	frRight.Visible = true
		end
	else
		userInputService.ModalEnabled = false
		allControlsOn()
		frLeft.Visible = false
		frRight.Visible = false
		end
	end)

Any help is appreciated.

Are you saying that you disbaled the screengui by setting the enabled property to false? If you did, that means that the script didn’t get moved under the player object under the gui, since the gui was disabled and didnt move to the PlayerGui, and so the script didn’t run.

I’m using a bool value to tell whether it is on or off.

1 Like

Ok, so when the bool value is set to false, you change the gui’s enabled property to false?

That doesn’t work as thats not the problem. The problem is that when it is enabled, it changes the movement and I don’t know how to change it back.