Attribute value change doesn't get noticed in a ServerScript

Hey! I’m trying to base off a function of an attribute, depending on its value. It’s a boolean value attribute, therefore if the value is true, something happens, if the value is false, something else happens.

It works perfectly fine if I change the attribute before playing the game (starting the local test in Roblox Studio), but nothing changes at all if I change the attribute while already playing the game (being in the local test). I am indeed doing it on the Server, but still, doesn’t work.

Even if I outprint it, it keeps printing that the value is for example false, even though it’s set to true on the server.

This is the part that is cruicial for this, ServerScript:

local function operateTrafficLightCollection(collection)
	local lights = collection:WaitForChild("TrafficLights"):GetChildren()
	local broken = collection:GetAttribute("Broken") or false
	local lightCount = #lights

	while true do
		if broken then
        else

local function startTrafficSystem()
	for _, collection in pairs(MainSystemFolder:GetChildren()) do
		if collection:IsA("Folder") then
			task.spawn(operateTrafficLightCollection, collection)
		end
	end
end

startTrafficSystem()

If you update from the client (local) script, the changes will only be visible for the client. If you update the attribute from the server script. The changes will be visible on both client and server

EDIT: Also, it seems like the script you have posted has typing issues

I know. As I said, I’m indeed changing it on the server. But it doesn’t work.

I assume this is where the issue of the code might be.
First off you are missing an end, three of them actually.

But other than that if broken then will always return false, thus never running. Which I assume what the problem is cause I cant really tell lol.

The reason this is happening is cause when you use Instance:GetAttribute('Key') it returns what the attribute is set to, after this point, the variable is never changed, so you would manually have to check if collection:GetAttribute("Broken") then, as again, the variable doesnt update.

1 Like

Hey! Thanks for the reply.

About the end parts missing - they’re not, as I said in the post, I only put the cruicial part of the script in there, not the whole script, so dw, in the real one it’s closed properly :sweat_smile:

Shouldn’t the if broken then check for true/false tho? Because the variable is set to the GetAttribute function and not the Instance itself, therefore it should return the value of the attribute, right?

Oh I basically just answered to this part - the variable is set to the GetAttribute already, I think you just misread the script :smiley: . All good tho, but I don’t understand why therefore it basically doesn’t change. Because it does check for the value and not the instance itself, and the value gets updated on the server side, therefore it should be noticed.

Oh wait I think I know what you meant now. The GetAttribute or false right? Well I put that there after it didn’t work with even just the GetAttribute function.

so if broken then automatically checks if the broken value is set to true or is not a nil value.

Basically when ever you define a variable to lets say like this:

local test = script:GetAttribute('test')

What this does is returns the current value of the attribute 'test', the thing about variables is that in the case of it being set to a value like a string, or a boolean, and many other value types.

All it does is get set to that value, not where the value came from or whatever, just that value.

For instance in the case we set it to an attribute, the variable is set to the current value of the attribute, not the attribute. Thus if the attributes value changes, the variable ignores that, because it was set to the value the attribute was when you defined the variable.

I have no idea if that made any sense, but TL;DR, once you define a variable to a certain value in the case of a Attribute, the variable will not update to match the current value of the Attribute.

So uh yeah, if you replace that snippet of code I showed earlier with this, it should work:

while true do
    if collection:GetAttribute('Broken') then

    else

    end
end
1 Like

Ohh you’re right. A silly mistake, but it makes sense now (it makes sense regardless, I just didn’t realize what was wrong). Thank you!

1 Like

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