Position Change GUI

  1. I’m am currently script my train right know and I wanna know how I can change the position of my GUI Object

  2. For example, If I press the W or S key on my keyboard the position of the GUI Object changes

  3. I tried using Anchor Part, and Udim2 and that didn’t

a = script.Parent.ScreenGui
local train = script.Parent
function onChildAdded(child)
	if child.Name == "SeatWeld" then
		if child.Part1.Name == "HumanoidRootPart" then
			local player = game.Players:GetPlayerFromCharacter(child.Part1.Parent)
			if player ~= nil then
				a.Parent = player.PlayerGui
			end
		end
	end
end

function onChildRemoved(child)
	if child.Name == "SeatWeld" then
		if a ~= nil then
			a.Parent = script.Parent
		end
	end
end

script.Parent.ChildAdded:connect(onChildAdded)
script.Parent.ChildRemoved:connect(onChildRemoved)

a.Frame.Foward.MouseButton1Click:connect(function()
train.Foward.Value = true
train.Neutral.Value = false
train.Reverse.Value = false
end)
a.Frame.Neutral.MouseButton1Click:connect(function()
train.Foward.Value = false
train.Neutral.Value = true
train.Reverse.Value = false
end)
a.Frame.Reverse.MouseButton1Click:connect(function()
train.Foward.Value = false
train.Neutral.Value = false
train.Reverse.Value = true
end)

if train.Throttle == 1 then
a.ThrottleNotchSlider.Knob.AnchorPoint = Vector2.new(0,-0.5)
end)
1 Like

Are you trying to tween the GUI Object’s position?

1 Like

You can use ContextActionService and bind a function to the Player actions associated with the W and S keys, which we can call “forward” and “backward”.

“forward” and “backward” are represented by two PlayerAction Enums.

Enum.PlayerActions.CharacterForward (W) and Enum.PlayerActions.CharacterBackward (S)

Starter Local Script code:

local ContextActionService = game:GetService("ContextActionService")

ContextActionService:BindAction(
    "PositionChange",
    function(_, inputState, inputObject) 
        -- affect your GUI accordingly and use the inputState/inputObject to your advantage
        -- such as changing your GUI Object's position
        return Enum.ContextAction.Pass -- this can be Pass or Sink, we want Pass so it doesn't stop anything else
    end,
    false, -- touch button creation (no need, so it is false)
    Enum.PlayerActions.CharacterForward,
    Enum.PlayerActions.CharacterBackward
)

Resources:
BindAction
ContextActionResult Enum
(Below are for the two parameters of the function, inputState & inputObject)
InputState
InputObject

Hello there, Gamemaker2478! If you want your GUI instance to move whenever a player press W or S, you’ll have to use the UserInputService service.

Local Script

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.W then
		[YOUR_FRAME_INSTANCE]:TweenPosition(
			UDim2.new(0, 0, 0, 0), -- Make sure to change the position!
			Enum.EasingDirection.In,
			Enum.EasingStyle.Sine,
			2
		)
	elseif inputObject.KeyCode == Enum.KeyCode.R then
		[YOUR_FRAME_INSTANCE]:TweenPosition(
			UDim2.new(0, 0, 0, 0), -- Make sure to change the position!
			Enum.EasingDirection.In,
			Enum.EasingStyle.Sine,
			2
		)
	end 
end)
5 Likes

I didn’t try tween because I thought that wasn’t gonna work