Help with function [2nd Attempt]

Hello there!

I am trying to add mobile support for a game, and I have run into a big issue.

I am using buttons to make the vehicle seat throttle move, like this.

throttle.MouseButton1Down:Connect(function()
	throttleMouse = true
	local direction = "Forward"
	local confirm = true
	while throttleMouse == true do
		print(throttleMouse)
		game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
		task.wait()
	end	
end)

The server then receives that and reacts accordingly(I won’t give the serverscript, as it is irrelevant to this issue).

This all works great until a player resets…

Because this script is a localscript and is in starterplayerscripts it does not reset like the rest of the gui(I need to keep it in playerscripts in this way for a variety of reasons).

So every time a player resets, the “throttle” needs to be reassigned.

This is what happens when the player first joins

local butttons = script.Parent.Parent.PlayerGui:WaitForChild("ScreenGui").MobileControls
local throttle = butttons.Throttle

And this is what happens when the script needs to reset the values

function onChange()
	wait(3)
	local newThrottle = script.Parent.Parent.PlayerGui.ScreenGui.MobileControls.Throttle
	throttle = newThrottle
end
game.ReplicatedStorage.ResetMControls.OnClientEvent:Connect(onChange)

You would expect that the throttle would now be the newThrottle, but not so.

The first function given above(The button click one) breaks and acts as if “throttle” is the old gui, which is now discarded when the player resets.

Proof this is the case is when I print throttle, and I click it before the player resets, it links it to the button in the explorer and shows me which one it is.

After the player resets and I click the print line it does not link it and doesn’t show me the button in the explorer.

I did not explain my first attempt very well at all, so I am trying now to explain my predicament better, so I really want to try to figure this out, any ideas?

If I need to explain something differently please tell me.

Try to create the MouseButton1Down event when there is a new throttle.

local butttons = script.Parent.Parent.PlayerGui:WaitForChild("ScreenGui").MobileControls
local throttle = butttons.Throttle

function throttleEvent()
	throttle.MouseButton1Down:Connect(function()
		throttleMouse = true
		local direction = "Forward"
		local confirm = true
		while throttleMouse == true do
			print(throttleMouse)
			game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
			task.wait()
		end	
	end)
end

function onChange()
	wait(3)
	local newThrottle = script.Parent.Parent.PlayerGui.ScreenGui.MobileControls.Throttle
	throttle = newThrottle

	throttleEvent()
end

game.ReplicatedStorage.ResetMControls.OnClientEvent:Connect(onChange)

throttleEvent()
2 Likes