Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would i go with positioning a mobile button with ContextActionService?

local variable = false
local p = game.Players.LocalPlayer

function toggle()
if variable == false then
variable = true
print(variable)
else
variable = false
print(variable)
end
end

game:GetService("ContextActionService"):BindActionToInputTypes("Toggle", toggle, true, "q") --The Q part sets the key q to do the same thing as the mobile button does. Change this to your liking.

p.PlayerGui.ContextActionGui.ContextButtonFrame.ContextActionButton.Position = Vector3.new(0,  0, 0, 0)

It prints out an error saying :

00:30:30.620 - ContextActionGui is not a valid member of PlayerGui
00:30:30.621 - Stack Begin
00:30:30.621 - Script 'Players.YBN_oSy.PlayerScripts.LocalScript', Line 16
00:30:30.622 - Stack End

If anybody knows a solution, please let me know

1 answer

✓ Accepted answer

Your error is being caused due to ContextActionGui returning nil. The script can not find it within the PlayerGui. This is probably caused by the script running before the gui has loaded.

p.PlayerGui:WaitForChild('ContextActionGui').ContextButtonFrame.ContextActionButton.Position = Vector3.new(0,  0, 0, 0)

The other issue is that you are trying to position the gui with Vector3.new. Instead you need to use UDim2.new.

Full Solution

local variable = false
local p = game.Players.LocalPlayer

function toggle()
if variable == false then
variable = true
print(variable)
else
variable = false
print(variable)
end
end

game:GetService("ContextActionService"):BindActionToInputTypes("Toggle", toggle, true, "q") 
p.PlayerGui:WaitForChild('ContextActionGui').ContextButtonFrame.ContextActionButton.Position = UDim2.new(0,  0, 0, 0)

1 comment

0

Thanks:)

This discussion is preserved as it was. New answers, comments, and votes are closed.