Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

TextButton not firing when pressed?

Asked by
hvnte_r 10
3 years ago

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)
0
Out of curiosity, is this a server-side script? DeceptiveCaster 3761 — 3y
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. hvnte_r 10 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
0
-1 for spoonfeeding. Please do not just give code without an explanation. DeceptiveCaster 3761 — 3y
0
You're using a function as in condition while it returns no values? CrunchChaotic 139 — 3y
0
That as well ^ DeceptiveCaster 3761 — 3y
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. hvnte_r 10 — 3y
0
:) genilsonotavio 132 — 3y
Ad

Answer this question