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
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.
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)
--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)