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)
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)