Why isn't this script working?

Greetings
I have created a script which is supposed to change a bool value when A or D is pressed. According to the bool value, an image texture changes.
Here is the script

--Services

local UIS = game:GetService("UserInputService")
local Run = game:GetService("RunService")

--Variables

local Sprite = script.Parent
local right = false

--Functions


local function SideHandler(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.A then
		right = false
	end
	if inputObject.KeyCode == Enum.KeyCode.D then
		right = true
	end
end

local function SideHandler()
	print(right)
	if right then 
		script.Parent.Image = "rbxassetid://5993496399"
	elseif not right then 
		script.Parent.Image = "rbxassetid://5993495870"
	end
end

--Loops


--Actions

UIS.InputBegan:Connect(SideHandler)
Run.Heartbeat:Connect(SideHandler)

However, if you run this script, nothing is happening.
And there are no errors

Please help me I am absolutely stuck.
Thnx

Did you link the function SideHandler to an event?

Both of your functions have the same name. Consider changing one of them to something else.

Since you have two functions going by the same name, running a function with that name will always result in running only one of them. The other function that doesn’t get ran has been overwritten by the other function, so it basically doesn’t exist to the computer.

3 Likes

Yes and On top of that it isn’t pointed to, add an event that fires the function

Example:

button.mouseclick:Connect(FunctionName) Links a button with variable “button”

1 Like

Maybe try this?

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gpe)
    if input.KeyCode == Enum.KeyCode.A then
        script.Parent.Image = "rbxassetid://5993496399"
    elseif input.KeyCode == Enum.KeyCode.D then
        script.Parent.Image = "rbxassetid://5993495870"
    end
end)
1 Like

Tried it and it worked, but it’s not optimised.

I have linked them if you read at the bottom.

Trying it right now, this might be the error.

And what about this one???

--Services

local UIS = game:GetService("UserInputService")

--Variables

local plr = game.Players.LocalPlayer
local plrGui = plr:WaitForChild("PlayerGui")
local Enviroment = plrGui:WaitForChild("Enviroment")
local GroundTexture = Enviroment.GroundTexture
local GoUp = false
local GoDown = false
local GoLeft = false
local GoRight = false

--Functions

local function Move(iO, gPE)
	if iO.KeyCode == Enum.KeyCode.W then
		GoUp = true
	end
	if iO.KeyCode == Enum.KeyCode.A then
		GoLeft = true
	end
	if iO.KeyCode == Enum.KeyCode.S then
		GoDown = true
	end
	if iO.KeyCode == Enum.KeyCode.D then	
		GoRight = true
	end
end

local function Stop(iO, gPE)
	if iO.KeyCode == Enum.KeyCode.W then
		GoUp = false
	end
	if iO.KeyCode == Enum.KeyCode.A then
		GoLeft = false
	end
	if iO.KeyCode == Enum.KeyCode.S then
		GoDown = false
	end
	if iO.KeyCode == Enum.KeyCode.D then	
		GoRight = false
	end
end



while wait() do
	if GoUp then
		GroundTexture.Position = GroundTexture.Position + UDim2.new(0,0,.001,0)
	elseif GoRight then
		GroundTexture.Position = GroundTexture.Position + UDim2.new(0.001,0,0,0)
	elseif GoDown then
		GroundTexture.Position = GroundTexture.Position + UDim2.new(0,0,-.001,0)
	elseif GoLeft then
		GroundTexture.Position = GroundTexture.Position + UDim2.new(-0.001,0,0,0)
	end
end


--Actions
UIS.InputBegan:Connect(Move)
UIS.InputEnded:Connect(Stop)

This one’s a bit easier to fix, you placed your while loop before assigning your input events.

Oh, so I need to do it at the bottom of my code right??

Yes, while loops run indefinitely. Any code bellow a while loop will not execute unless you break out of the while loop.

Just assign your input events before the while loop, and you’ll be good to go.

1 Like

Ok thnx mate, I appreciate it.

1 Like