Attribute keeps resetting?

I was working with using attributes and for some reason there’s like a 50/50 chance that they actually stay before going ‘nil’ for some reason?

I’m trying to just add like a ‘ControlsEnabled’ attribute that I can toggle when I need to, I started running into issues using a LocalScript requiring the controls module so I forked the system and it still had the issue.

Code in question: (i lost my sanity trying to write this ok)

	local checkControls = function(state)
		print(state)
		wait()
		print(Players.LocalPlayer:GetAttribute("ControlsEnabled"))
	end
	
	local cc = function()
		task.spawn(checkControls,Players.LocalPlayer:GetAttribute("ControlsEnabled"))
	end
	
	if(Players.LocalPlayer:GetAttribute("ControlsEnabled") == nil) then
		Players.LocalPlayer:SetAttribute("ControlsEnabled",true)
	end
	
	Players.LocalPlayer:GetAttributeChangedSignal("ControlsEnabled"):Connect(cc)
	cc()

Sometimes it prints: true, true which is expected and other times it prints true, like what happened to the attribute? Is my Roblox just cursed or? Does anyone have any ideas? It also doesn’t matter what it’s named for me.

image

I thought maybe I did something wrong with another script so I created a blank baseplate and tried it and it still did the same thing.

I created a reproduction if people can’t get the same behavior: https://www.roblox.com/library/8405544288/PlayerModule

1 Like

I was never able to fix it on the client, but I fixed it with this code on the server:

local players = game:GetService("Players");

local onPlayer = function(player)
	player:SetAttribute("ControlsEnabled",true);
end

players.PlayerAdded:Connect(onPlayer);
for _,player in pairs(players:GetPlayers()) do
	task.spawn(onPlayer,player);
end
1 Like