Textbuttons created with a script don't work

For example, the actiavted event doesn’t work. Also the background doesn’t show up


https://gyazo.com/11dd4faed4e79a2a170eb37929ef281d

here is my script:

local UserInputService = game:GetService(“UserInputService”)

local Flying = false

local TimesSpacedBarPressed = 0

local Player = game.Players.LocalPlayer

UserInputService.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.Space then
TimesSpacedBarPressed = TimesSpacedBarPressed + 1
if Flying == false then
if TimesSpacedBarPressed >= 2 then
local BodyForce = Instance.new(“BodyForce”)
BodyForce.Parent = Player.Character:WaitForChild(“HumanoidRootPart”)
BodyForce.Force = Vector3.new(10,4000,10)
BodyForce.Name = “Force”
Flying = true

			local Gui = Instance.new("ScreenGui")
			Gui.Parent = Player.PlayerGui
			Gui.Name = "FlyingGui"
		
			
			local CancelFlying = Instance.new("TextButton")
			CancelFlying.Text = "Stop flying"
			CancelFlying.Parent = Gui
			CancelFlying.Position = UDim2.new(0.8,0,0.5,0)
			
			
			CancelFlying.Activated:Connect(function()
				Flying = false
				CancelFlying.Parent:Destroy()
				if game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart"):FindFirstChild("Force") then
			 		game.Players.LocalPlayer.Character.HumanoidRootPart.Force:Destroy()
				end
				TimesSpacedBarPressed = 0
			end)
			
		end
	end

end

end)

1 Like

I have figured it out here is the Full Code

local UserInputService = game:GetService("UserInputService")

local Flying = false

local TimesSpacedBarPressed = 0

local Player = game.Players.LocalPlayer

UserInputService.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.Space then
TimesSpacedBarPressed = TimesSpacedBarPressed + 1
if Flying == false then
if TimesSpacedBarPressed >= 2 then
local BodyForce = Instance.new("BodyForce")
BodyForce.Parent = Player.Character:WaitForChild("HumanoidRootPart")
BodyForce.Force = Vector3.new(10,4000,10)
BodyForce.Name = "Force"
Flying = true

			local Gui = Instance.new("ScreenGui")
			Gui.Parent = Player.PlayerGui
			Gui.Name = "FlyingGui"
		local CopyFrom = script.Parent.CopyButton

			
			local CancelFlying = Instance.new("TextButton")
			CancelFlying.Text = "Stop flying"
			CancelFlying.Parent = Gui
				CancelFlying.Position = UDim2.new(0.8,0,0.5,0)
CancelFlying.Size = UDim2.new(0, CopyFrom.AbsoluteSize.X, 0, CopyFrom.AbsoluteSize.Y)
CancelFlying.Rotation = CopyFrom.AbsoluteRotation
			
			CancelFlying.MouseButton1Click:Connect(function()
				Flying = false
				CancelFlying.Parent:Destroy()
				if game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart"):FindFirstChild("Force") then
			 		game.Players.LocalPlayer.Character.HumanoidRootPart.Force:Destroy()
				end
				TimesSpacedBarPressed = 0
			end)
			
		end
	end

end
end)

what i did was made button and named it copy button which u will notice in line 23 so what you should do is create a button in screengui and name it CopyButton Like in the Image.
Screenshot_94
then the button we will create will take the size and absolute size from the copy button we made. If you would like to ask anything tell me :smiley:
Edit: I was wrong you can use Activated if you would like to sorry for the confusion

1 Like