Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

Why won't this simple RemoteEvent script work?

Asked by 5 months ago
Edited 5 months ago

So basically I'm trying to make it so whenever you click something using a ClickDetector the TextLabels inside my LabelGui go invisible. Thing is, nothing happens, and the Output doesn't show any errors.

I have it all set out properly, and I've debugged the Script, yet it doesn't work. The printing in the debugging works but the line that is supposed to make the TextLabels invisible isn't.

Any help would be appreciated!

Script - Located in the Model going to be clicked

local remote = game:GetService("ReplicatedStorage").Events.ClickEvent

script.Parent.ClickDetector.MouseClick:Connect(function()
    for i,player in pairs(game.Players:GetPlayers()) do
        remote:FireClient(player, "eventClick")
    end
end)

-- I don't need to debug this since it works in firing the `LocalScript`, meaning it works perfectly.

Click(LocalScript) - Located in PlayerGui.Clicks.Click (Clicks is a Folder, Click is the LocalScript)

local remote = game:GetService("ReplicatedStorage").Events.ClickEvent
local getchildren = script.Parent.Parent:WaitForChild("LabelGui")

remote.OnClientEvent:Connect(function(eventClick)
    if eventClick == "eventClick" then
        getchildren:GetChildren().Visible = false
        print("yolo") -- Debugging to see if it works
    end
end)

If you'd like to see more information and where I've placed my Events and Scripts, LocalScripts, etc then let me know and I'll post it in the Comments. I haven't posted it in this because I don't think it is the issue.

Thanks!

EDIT : I took out the WaitForChild for the getchildren variable and it gave me the error, LabelGui is not a valid member of 'PlayerGui'. Is this helpful? Line 2 on the LocalScript is the error for the getchildren line not working.

2 answers

Log in to vote
1
Answered by
incapaxx 3172 Badge of Merit Moderation Voter Community Moderator
5 months ago
Edited 5 months ago
local remote = game:GetService("ReplicatedStorage").Events.ClickEvent
local getchildren = script.Parent.Parent:WaitForChild("LabelGui")

remote.OnClientEvent:Connect(function(eventClick)
    if eventClick == "eventClick" then
        getchildren:GetChildren().Visible = false
        print("yolo") -- Debugging to see if it works
    end
end)

Did you think that this code was going to set the Visible property of LabelGui's children to false? You thought wrong. The :GetChildren() method returns an array, arrays are mappings from 1...#array to arbitrary values, and you can get an element from the array with a numerical key, say 2 (array[2]), and you are trying to set Visible, a string key. You can iterate over that array with a generic for loop.

for _, child in ipairs(getchildren:GetChildren()) do
    -- # `child` is the current child you are working with
    if child.Name == "name here" then
        child.Visible = false
    elseif child.Name == "other name" then
        child.Visible = true
    end
end

Additionally, instead of calling :FireClient() individually for each player you can just call :FireAllClients().

0
Okay thanks, but what if I have 2 textlabels and i want one to be invisible and the other to be visible, what would I do TheOnlySmarts 160 — 5mo
0
I read your comment on gioni01's answer where you wanted one to be invisible and the other to be visible. My method checks the name property, but you can make any check you like. incapaxx 3172 — 5mo
0
okay thanks!! TheOnlySmarts 160 — 5mo
Ad
Log in to vote
1
Answered by
gioni01 2055 Moderation Voter Community Moderator
5 months ago
Edited 5 months ago

:GetChildren() returns a table, and you're trying to set the Visible property (which obviously doesn't exist in tables) to false. What you need to do is loop through the children. Also, it worries me that this did not give you an error, because it should have. Are you sure the server script is running?

local remote = game:GetService("ReplicatedStorage").Events.ClickEvent
local Gui = script.Parent.Parent:WaitForChild("LabelGui")

remote.OnClientEvent:Connect(function(eventClick)
    if eventClick == "eventClick" then
        for _,v in pairs(Gui:GetChildren()) do
            v.Visible = false
        end
    end
end)

Resources:

:GetChildren()

Accept and upvote if this helps!

0
Thanks this helps but how would I one textlabel's invisible = false and another = true, because i have 2 textlabels, and i want one to be invisible and the other to be visible, so how could i make this work, i thought of using a IsA function but idk now TheOnlySmarts 160 — 5mo
0
you could do a couple of things. you could check if one's name is equal to something, and if so, make it either visible or invisible. you could also create values inside those textlabels, and if that value is true, make the gui true etc gioni01 2055 — 5mo
0
yeah but if i did, 'if v.Value == one then v.Visible = true' what would I do to make the other invisible, also does the getchildren only choose one of the 2, like a math.random, or does it choose both, if it chooses both, how would i make one visible and the other invisible if im using the same parameter(aka. the 'v') TheOnlySmarts 160 — 5mo
0
thanks this helped, but incapaxian beat u to it in explaining it how i wanted it, thankyou so much though, i still upvoted u for helping me! TheOnlySmarts 160 — 5mo

Answer this question