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

TextButton not firing when pressed?

I have a TextButton in a ScreenGui which is only visible to certain playerIDs. When pressed, it's meant to move a brick by the name of Void1 from game.Lighting to workspace, and then do the same thing with a brick named Void2, then change the text and background color of the button. When pressed again, it's meant to do the same thing in reverse.

However, upon testing, when I press the button, nothing happens. This is odd, because there aren't any errors either. I'm not sure if there's anything I'm doing wrong, if so please let me know. I'm very new to Lua.

function parentPressed()
	game.Lighting.Void1.Parent = workspace
	game.Lighting.Void2.Parent = workspace
	script.Parent.Text = "MidVoid: Enabled"
	script.Parent.BackgroundColor3 = Color3.fromRGB(116, 255, 65)
end
script.Parent.MouseButton1Down:connect(parentPressed)

if parentPressed() == true then
function parentPressedAgain()
		game.Workspace.Void1.Parent = game.Lighting
		game.Workspace.Void2.Parent = game.Lighting
		script.Parent.Text = "MidVoid: Disabled"
		script.Parent.BackgroundColor3 = Color3.fromRGB(255, 57, 57)
end
script.Parent.MouseButton1Down:connect(parentPressedAgain)

2 comments

0

Out of curiosity, is this a server-side script?

0

Yes, sorry for not responding. It's a server-side script rather than a localscript. I now realize that it needs to be done with a localscript that fires a RemoteEvent, I figured that out soon after I created the post, so I kinda just ditched this idea altogether.

1 answer

✓ Accepted answer

PS: If you are a complete beginner, I recommend that you study RemoteEvents and RemoteFunctions before reading this solution. I'm sorry if this code got too messed up.

This is because your script is being executed on the player's side, that is, your changes will not be made on the server in general.

You need to use a RemoteEvent to inform the server when your player clicks the button, and a Script on ServerScriptService that will perform this action.

In the TextButton.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent1 = ReplicatedStorage:WaitForChild("RemoteEvent1")
local RemoteEvent2 = ReplicatedStorage:WaitForChild("RemoteEvent2")

function parentPressed()
	RemoteEvent1:FireServer() -- To put the parts in Workspace.
	script.Parent.Text = "MidVoid: Enabled"
	script.Parent.BackgroundColor3 = Color3.fromRGB(116, 255, 65)
end

script.Parent.MouseButton1Down:connect(parentPressed)	 

if parentPressed() == true then
	function parentPressedAgain()
		script.Parent.Text = "MidVoid: Disabled"
		script.Parent.BackgroundColor3 = Color3.fromRGB(255, 57, 57)

		RemoteEvent2:FireServer() -- To make changes to the workspace.
	end
end

script.Parent.MouseButton1Down:connect(parentPressedAgain)

In a Script inside ServerScriptService.

The Script that serves the RemoteEvent1.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent1 = ReplicatedStorage:WaitForChild("RemoteEvent1")

RemoteEvent1.OnServerEvent:Connect(function()
	game.Lighting.Void1.Parent = workspace
	game.Lighting.Void2.Parent = workspace
end)
	

The Script that serves the RemoteEvent2.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent2 = ReplicatedStorage:WaitForChild("RemoteEvent2")

RemoteEvent2.OnServerEvent:Connect(function()
	game.Workspace.Void1.Parent = game.Lighting
	game.Workspace.Void2.Parent = game.Lighting
end)

5 comments

0

-1 for spoonfeeding. Please do not just give code without an explanation.

0

You're using a function as in condition while it returns no values?

0

That as well ^

0

Thank you for this answer, I realized shortly after I made the post that this was the case. I'll look more into RemoteEvents at a later date but for now it seems too complicated for me.

0

:)

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