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

Functioning MOVING TextButton gui. What's wrong with the script I have right now?

The function of this script is well, nothing much. The gui originally is telling the player to not click it. Then when clicked it should display "I've been clicked!" then, after one second, say "Don't click me (count that goes up each time it's been clicked)."

How would I go about changing it from what it is right now for it to work?

local screenGui = Instance.new("ScreenGui")
screenGui.Parent = game.Players.LocalPlayer.PlayerGui
r=1
local textButton = Instance.new("TextButton")
textButton.Parent = screenGui
textButton.Size = UDim2.new(0, 300, 0, 100)
textButton.Text = "Don't click me, " .. game.Players.LocalPlayer.Name .. "!"

local xPosition = 1
local speed = 0.005
while true do
	xPosition = xPosition - speed
	textButton.Position = UDim2.new(xPosition, 0, 0, 0)
	if xPosition < -0.1 then
		xPosition = 1
	end
	wait()
end
 
textButton.MouseButton1Down:connect(function()
	textButton.Text = "I've been clicked!"
	wait(1)
	textButton.Text = "Don't Click Me Again!" .. r + 1
end)

Also, is there any way to make this script smaller/more efficient or well made? And finally, how would I change the font size, and type?

EDIT: How can I also reference something in a script from another script?

1 answer

The last question should be a seperate question. A quick answer for that is: BindableEvents/Functions, RemoteEvents/Functions, and value objects (IntValue, StringValue, etc.).

-- I like to define the variables at the top and categorise them
local Player = game.Players.LocalPlayer

local Gui = Instance.new("ScreenGui")
Gui.Parent = Player.PlayerGui
local Button = Instance.new("TextButton")
Button.Parent = Gui
Button.Size = UDim2.new(0,300,0,100)
Button.Text = "Don't click me, " ..Player.Name.. "!"

local Clicks = 0
local CanClick = true
local Position = 1
local Speed = 0.005

Button.MouseButton1Clicked:connect(function()
	if not CanClick then return end -- debounce
	CanClick = false

	Button.Text = "I've been clicked!"
	wait(1)
	Clicks = Clicks + 1
	Button.Text = "Don't click me again! " ..Clicks

	CanClick = true
end)

while wait() do -- will infinitely stop the code so put it under everything else
	Position = Position - Speed
	Button.Position = UDim2.new(Position,0,0,0)
	if Button.AbsolutePosition.X <= -100 then -- use AbsolutePosition because scale -0.1 is not guaranteed to be offset -100
		Position = 1
	end
	-- an alternative to this movement method could be tweening
end

This should work better. Hope I helped!

~TDP

2 comments

0

I'm getting an error: "Clicked is not a valid member of TextButton"

0

Ah, my bad. I will edit.

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