Hey there!
I am having a little trouble with these mobile control buttons.
My issue is every time a player resets themselves, the player gui is reset, so the buttons(throttle, brake, etc) need to be called again, otherwise the scripts are trying to connect to gui that is discarded. I did this in the onChange() function. The problem is that the rest of the script won’t recognize the change. I reassign it like this.
throttle = newThrottle
brake = newBrake
left = newLeft
right = newRight
(More details in the full script)
But all of the functions will then act like nothing happened and still try to get the old buttons, which are no longer there.
throttle.MouseButton1Down:Connect(function()
print("InputBegan")
print(throttle)
throttleMouse = true
local direction = "Forward"
local confirm = true
while throttleMouse == true do
print(throttleMouse)
game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
task.wait()
end
end)
This script is a local script is in the playerscripts folder.
Full Script
local UserInputService = game:GetService("UserInputService")
local throttleMouse = false
local brakeMouse = false
local leftMouse = false
local rightMouse = false
local butttons = script.Parent.Parent.PlayerGui:WaitForChild("ScreenGui").MobileControls
local throttle = butttons.Throttle
local brake = butttons.Brake
local left = butttons.Left
local right = butttons.Right
function onChange()
wait(3)
local newThrottle = script.Parent.Parent.PlayerGui.ScreenGui.MobileControls.Throttle
local newBrake = script.Parent.Parent.PlayerGui.ScreenGui.MobileControls.Brake
local newLeft = script.Parent.Parent.PlayerGui.ScreenGui.MobileControls.Left
local newRight = script.Parent.Parent.PlayerGui.ScreenGui.MobileControls.Right
print(newThrottle)
print(newBrake)
print(newLeft)
print(newRight)
butttons = script.Parent.Parent.PlayerGui:WaitForChild("ScreenGui").MobileControls
throttle = newThrottle
brake = newBrake
left = newLeft
right = newRight
throttleMouse = false
brakeMouse = false
leftMouse = false
rightMouse = false
print(throttle)
end
game.ReplicatedStorage.DeactivateMControls.OnClientEvent:Connect(onChange)
throttle.MouseButton1Down:Connect(function()
print("InputBegan")
print(throttle)
throttleMouse = true
local direction = "Forward"
local confirm = true
while throttleMouse == true do
print(throttleMouse)
game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
task.wait()
end
end)
throttle.InputEnded:Connect(function()
print("InputEnded")
local direction = "Forward"
local confirm = false
throttleMouse = false
game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
end)
brake.MouseButton1Down:Connect(function()
print("InputBegan")
brakeMouse = true
local direction = "Backward"
local confirm = true
while brakeMouse == true do
game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
task.wait()
end
end)
brake.InputEnded:Connect(function()
print("InputEnded")
local direction = "Backward"
local confirm = false
brakeMouse = false
game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
end)
left.MouseButton1Down:Connect(function()
print("InputBegan")
leftMouse = true
local direction = "Left"
local confirm = true
while leftMouse == true do
game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
task.wait()
end
end)
left.InputEnded:Connect(function()
print("InputEnded")
local direction = "Left"
local confirm = false
leftMouse = false
game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
end)
right.MouseButton1Down:Connect(function()
print("InputBegan")
rightMouse = true
local direction = "Right"
local confirm = true
while rightMouse == true do
game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
task.wait()
end
end)
right.InputEnded:Connect(function()
print("InputEnded")
local direction = "Right"
local confirm = false
rightMouse = false
game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
end)
while true do
print(throttle)
wait(1)
end