What do you want to achieve?
I would like my script to be able to create a table of values based on if a corresponding text button has an attribute (currently “targetUser”) set to true.
What is the issue?
The issue is that I have seen that the attribute is true and the script still thinks it isn’t true. Both scripts (the script setting the attribute and the script checking the attribute) are server scripts so that shouldn’t be the problem.
What solutions have you tried so far?
I have tried print debugging and I have checked what the attribute shows using the explorer in game and it says that targetUser is true.
Script that sets the attribute (“Handler”)
for _,v in pairs(script.Parent.Users:GetChildren()) do
if v.ClassName == "TextButton" then
v.MouseButton1Click:Connect(function()
print(v:GetAttribute("targetUser"))
if v:GetAttribute("targetUser") == true then
print("selected, unselecting")
v:SetAttribute("targetUser",false)
v.BackgroundColor3 = Color3.fromRGB(0, 127, 156)
elseif v:GetAttribute("targetUser") == false then
print("unselected, selecting")
v:SetAttribute("targetUser",true)
v.BackgroundColor3 = Color3.fromRGB(0, 156, 112)
end
end)
end
end
Background colors work by the way
Script that checks the value (“Script”):
local selectedUsers = {}
for _,v in pairs(script.Parent.Parent.Parent.Users:GetChildren()) do
if v.ClassName == "TextBox" and v.Name ~= "Template" then
print(v:GetAttribute("targetUser"))
if v:GetAttribute("targetUser") == true then
print(v.Name.." is selected")
table.insert(selectedUsers,v.Text)
end
end
end
print(selectedUsers)
The print statement prints “{}”, indicating that the script doesn’t think that the value is true, as well as the length of the table prints as 0.
Explorer screenshot (“Handler” is automatically enabled when a player uses the admin command, I have checked that it gets enabled. That isn’t the problem):
The issue is that the script that checks the value searches a child that has ClassName “TextBox” and a name NOT equal to “Template”. And the child you are searching has name “Template”.
You can simply fix it:
local selectedUsers = {}
for _,v in pairs(script.Parent.Parent.Parent.Users:GetChildren()) do
if v.ClassName == "TextBox" and v.Name == "Template" then
print(v:GetAttribute("targetUser"))
if v:GetAttribute("targetUser") == true then
print(v.Name.." is selected")
table.insert(selectedUsers,v.Text)
end
end
end
print(selectedUsers)
I did not give a logic change, the code is just shorter.
If you print all the attributes of the object with :GetAttributes, what does it print? You should also add a print when the handler after :SetAttribute is called, and then other in the second script before :GetAttribute is called to make sure there isn’t a race condition. Send a screenshot of the output after running the code with the debug prints.
Both scripts (the script setting the attribute and the script checking the attribute) are server scripts so that shouldn’t be the problem.
I do not think that is true. MouseButton1Click() is strictly local event (server cannot know if player - even admin - has clicked a button without some kind of remote event).
Therefore I am certain that the attribute is set on client, and due to filtering, it is not on server. You can switch to server by clicking the second button:
Once you do that you will see for yourself that the value is not set on server.
This aided me in finding the issue (incorrect class name).
Tbh I probably wouldve been staring at the code for a few more days if it wasnt for your help