Script In Denial of Attribute Values

  1. 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.
  2. 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.
  3. 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):

1 Like

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)

Reply and correct me if I’m wrong. :slight_smile:

I forgot to mention that the template it cloned and the name is changed to the player’s username. Thanks for trying to help though.

1 Like

When does the script that checks the attributes run? Are you sure it’s not running before the attributes are set?

It’s printing nil in Script, right?

There’s no point in having the pairs anymore, and why is this script so deep in the hierarchy when it needs to access something further up?

local UsersContainer = script.Parent.Parent.Parent

for _, User in UsersContainer:GetChildren() do
    ...
end

You can also use String Interpolation instead String Concatenation, which I think looks better:

local MyString = `Value: {Value}`

-- Instead of

local MyString = "Value: " .. tostring(Value)

I know that it is set before the script checks because I have looked at the value before the script checks and it is set to true

Okay, I used the script you provided instead

I would rather not change the way I do this.

Here is what happened (same thing as before, nothing fixed).

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:
image

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 :skull:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.