Would while wait(0.1) do lag?

Ok, so I have done a script where it will use while wait(0.1) do to see if a certain value has changed, because I’m using datastore this is what I think is necessary, although I would like to know if this would lag.

Here’s the script

while wait(0.1) do
	if stopAnnounce.Value == 1 then
		script.Parent.X.Visible = true
	elseif stopAnnounce.Value == 0 then
		script.Parent.X.Visible = false
	end
end

If there is a better way to do this please do let me know

It would be much better to use GetPropertyChangedSignal to detect when a value has changed.

value:GetPropertyChangedSignal("Value"):Connect(function()
    -- do stuff
end)
1 Like

im using datastore so i dont think that it would detect the first time when it is inserted by the datastore itself

As @BPilot253 mentioned, you can use GetPropertyChangedSignal OR Changed if the thing is a BaseValue, which it is for your case, and is better since you want to use the new value

value.Changed:Connect(function(newVal)

end)

@CheapVindaloo It will detect if you start the value as 0, and then connect the event up and THEN change the value

1 Like

It would detect any change in the value as long as you update the value after you have connected the GetPropertyChangedSignal event

I used .changed before when the value was inserted by the datastore value.Changed would not see that and act as if the value was at 0 even though it was at 1

Could you show the script you are using for the data store?

Here it is,

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("datastore")

local function saveData(player)

	local tableToSave = {
		player.vals.stopannounce.Value; 
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) 
	end)

	if success then 
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(function(player)


	local vals = Instance.new("Folder")
	vals.Name = "vals"
	vals.Parent = player

	local stopannounce = Instance.new("IntValue")
	stopannounce.Name = "stopannounce"
	stopannounce.Parent = vals

	local data 
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId) 

	end)

	if success then

		stopannounce.Value = data[1] 

	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") 
	end

end)

game.Players.PlayerRemoving:Connect(function(player) 
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			saveData(player)
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

Did you try putting the Changed event after the creation of stopannounce?

local stopannounce = Instance.new("IntValue")
stopannounce.Name = "stopannounce"
stopannounce.Parent = vals

stopannounce.Changed:Connect(function(newVal)
	--Code
end)

the datastore script is a completely different script, would I fireclient?

Most likely the server loads and updates the value before the client fully loads in. I would suggest having an “Initialise” function on the client that would read the value at first and then hook up a changed event for future changes. This is what I do for my projects.

2 Likes