Script can't set DataStore variable to true

Hey, im trying to access with a other “Server-Script” a variable from my "DataStore “Server-Script” so i can set it from false to true.

the problem seems to be an Communicating issue between my “Server-Script” & "DataStore “Server-Script” while my “Server-Script” is printing (“print("Timeout was successfully changed to true and saved in the Data Store")”) no Signal can be detected at the "DataStore “Server-Script”'s end,

This is the DataStoreHandler Script like the name allready says it handels the player data.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local PlayerUserID = Player.UserId
	print(PlayerUserID)   
	key = "EndTime" 

	local startTime = os.time()
	endTime = myDataStore:GetAsync(key) or (startTime + 10 * 60)
	myDataStore:SetAsync(key, endTime)

	local Timeout = false

	local function afterLoop()
		local Data
		local success, errormessage = pcall(function()
			Data = myDataStore:GetAsync(PlayerUserID, key, endTime)
		end)
		if success then
			print("Data saved successfully")
		else
			print("There was an error!")
			warn(errormessage)
		end
	end

	coroutine.wrap(afterLoop)()

	repeat
		while os.time() < endTime do
			local timeLeft = endTime - os.time()
			local minutes = math.floor(timeLeft / 60)
			local seconds = timeLeft % 60
			print(string.format("Time Left: %02d:%02d", minutes, seconds))
			wait(1)

			-- Überprüfen des Wertes von "Timeout" im Data Store
			local success, err = pcall(function()
				Timeout = myDataStore:GetAsync("Timeout")
			end)

			if not success then
				warn("Error retrieving timeout from data store:", err)
			end
		end

		if Timeout then
			print("Time is over!")
			myDataStore:RemoveAsync(key)
		else
			print("Timeout disabled")
		end
	until not Timeout
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local PlayerUserID = Player.UserId   
	local success, errormessage = pcall(function()

		myDataStore:GetAsync(PlayerUserID, key, endTime)
	end)
	if success then
		print("Last Data saved successfully")
	else
		print("There was an error!")
		warn(errormessage)
	end
end)

This is the Script that should set the Timeout variable from false to true.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")

-- Retrieve the variable "Timeout" from the Data Store
local Timeout
local success, err = pcall(function()
	Timeout = myDataStore:GetAsync("Timeout")
end)

-- Check if the retrieval was successful
if success then
	-- Change the value of "Timeout" to true
	Timeout = true

	-- Save the changed value of "Timeout" in the Data Store
	local success, err = pcall(function()
		myDataStore:SetAsync("Timeout", Timeout)
	end)

	-- Check if the saving was successful
	if success then
		print("Timeout was successfully changed to true and saved in the Data Store")
	else
		warn("Error saving Timeout in the Data Store:", err)
	end
else
	warn("Error retrieving Timeout from the Data Store:", err)
end

if any more information is needed to debug this issue feel free to reach out about it and like always help very appreciated.

  1. Check your output, is there anything out the ordinary? Place as many warns/prints in your code’s conditional statements or tasks to see where it stops.
  2. If that doesn’t work, I believe DataStores cannot save certain values, although last time I checked they could store booleans. Try testing this using strings instead (“true” and “false” or anything else)
  3. Double check that RemoteEvents are properly put into place and that they are being fired.

I already tested these things. also to access any Data from a “Data store” no Remote Events are needed. “DataStore Documentation”

My apologies, I must have gotten part of your post incorrect with RemoteEvents. Could I please have any further information on what you’re trying to achieve in your code?

No problem, anyways i want to access a variable called: “Timeout” out of my “DataStores” this, variable is set defaultly to false inside of my “DataStores” so my other “Server-Script” that is trying to access the variable “Timeout” sets it to true it even prints the following: ("print("Timeout was successfully changed to true and saved in the Data Store")")but at the "DataStore “Server-Script”'s end, no signal can be detected.

Hm, maybe try this?

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")

-- Retrieve the variable "Timeout" from the Data Store
local Timeout
local success, err = pcall(function()
	Timeout = myDataStore:GetAsync("Timeout")
end)

-- Check if the retrieval was successful
if success then
    if (Timeout == true) then print("Already true!") return end
	local success, err = pcall(function()
		myDataStore:SetAsync("Timeout", { true })
	end)

    local newVal
    local s, e = pcall(function()
        newVal = myDataStore:GetAsync("Timeout")
    end)

	-- Check if the saving was successful
	if success and s then
		print("Timeout was successfully changed to: ".. tostring(newVal[1]) .." and saved in the Data Store")
	else
		warn("Error saving Timeout in the Data Store:", err, "Also maybe; ", tostring(e))
	end
else
	warn("Error retrieving Timeout from the Data Store:", err)
end

does not work it says its already true while it’s not. i think my Data stores are having trouble detecting the updated variable.

Just to check the scoping isn’t the issue, you’ve redeclared success

if success then
	-- Change the value of "Timeout" to true
	Timeout = true

	-- Save the changed value of "Timeout" in the Data Store
	local success, err = pcall(function()
		myDataStore:SetAsync("Timeout", Timeout)
	end)

	-- Check if the saving was successful
	if success then
		print("Timeout was successfully changed to true and saved in the Data Store")
	else
		warn("Error saving Timeout in the Data Store:", err)
	end
else
	warn("Error retrieving Timeout from the Data Store:", err)
end

Perhaps more importantly, is any of this running? Do you get any print statements from it?
It repeats until not Timeout (which is false already), but also is the os.time() still less than the endTime at this point, as this would prevent the while loop from getting the updated value of Timeout from the DataStore?

repeat
		while os.time() < endTime do
			local timeLeft = endTime - os.time()
			local minutes = math.floor(timeLeft / 60)
			local seconds = timeLeft % 60
			print(string.format("Time Left: %02d:%02d", minutes, seconds))
			wait(1)

			-- Überprüfen des Wertes von "Timeout" im Data Store
			local success, err = pcall(function()
				Timeout = myDataStore:GetAsync("Timeout")
			end)

			if not success then
				warn("Error retrieving timeout from data store:", err)
			end
		end
--Repeat will only run from here...
		if Timeout then
			print("Time is over!")
			myDataStore:RemoveAsync(key)
		else
			print("Timeout disabled")
		end
	until not Timeout

It Starts running when “Timeout” will be set true so since “Timeout” is false at the time no.

No, it repeats until Timeout is set to true

im honestly, not sure about this one.

At this point I’d suggest a full rewrite.

This will not help me at all. since this code is already very complicated from my view and took about 5+ hours.

The code actually starts running when a new player is added
game.Players.PlayerAdded:Connect(function(Player)

This part of code sets an end time only if one has never been set, otherwise it gets whatever the first one was saved as and then sets it again.
endTime = myDataStore:GetAsync(key) or (startTime + 10 * 60) myDataStore:SetAsync(key, endTime)

Try this instead (not tested), I’m not sure you would need the second script you posted in OP, and I’m not sure how you have set up anything else or what it is used for.

game.Players.PlayerAdded:Connect(function(Player)
	local PlayerUserID = Player.UserId
	print(PlayerUserID)   
	local key = "EndTime" 

	local startTime = os.time()
	endTime = startTime + (10 * 60)
	myDataStore:SetAsync(key, endTime)

	local Timeout = false
	--Commented out as it only prints to output.
	--[[ 
	local function afterLoop()
		local Data
		local success, errormessage = pcall(function()
			Data = myDataStore:GetAsync(PlayerUserID, key, endTime)  --Not sure you can call 3 keys at a time? and either way there is only 1 variable declared to receive them.
		end)
		if success then
			print("Data saved successfully")
		else
			print("There was an error!")
			warn(errormessage)
		end
	end

	coroutine.wrap(afterLoop)()
]]

	while os.time() < endTime  do
		local timeLeft = endTime - os.time()
		local minutes = math.floor(timeLeft / 60)
		local seconds = timeLeft % 60
		print(string.format("Time Left: %02d:%02d", minutes, seconds))
		wait(1)
	end
	myDataStore:SetAsync("Timeout", true)
	repeat
		task.wait(4) --may be required to allow for datastore caching
		local success, err = pcall(function()
			Timeout = myDataStore:GetAsync("Timeout")
		end)
		print(Timeout) --Could return false, as the get is called too soon after the set.
		if success then
			if Timeout then
				print("Time is over!")
				myDataStore:RemoveAsync(key)
			else
				print("Timeout disabled")
			end
		else
			warn("Error retrieving timeout from data store:", err)
		end
	until Timeout
end)

Not to be disrespectful but this doesn’t seem to do anything it rather breaks everything instead.

Okay… what’re you trying to achieve in your code? As in the actual idea.

in which Code we still need to work with 2 Scripts. also, please clarify your question of the things you dont understand in my idea since i already stated it several times.

What does the code do- I understand that it attempts to set the Timeout to true and false but I can’t read what it exactly is trying to do. Does each player have one, is it a timer for an update? I can’t tell.

Since you didnt clearify the Code i just assume you me my "DataStore “Server-Script” that has a Countdown that should act as like a Timeout (“Not the Variable!”) to block a User (“The Player”) from doing certain things in my for 10 mins Game.

for more information feel free to ask futher.

uh maybe its because you’re storing the key as a global key not self - assigned to a specific user??

How would i be able to change this? sorry i’m pretty new to Data Stores. :sweat_smile:


also just a question but does this Change anything on my “Timeout” variable being able to set to true?

Hey everyone & @Wigglyaa, @jasperagent_dev, i actually think i Fixed the main issue of my DataStores,
because i actually did forget to include my "Timeout “Variable” in SetAsync().


However i still seem to Run into an issue with Detecting that the Variable has Updated.


This is the the Updated "DataStore “Server-Script” and “Server-Script”: …

DataStore’s:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local PlayerUserID = Player.UserId
	print(PlayerUserID)   
	--Load Data
	key = "EndTime" 

	local startTime = os.time()
	endTime = myDataStore:GetAsync(key) or (startTime + 10 * 60)
	myDataStore:SetAsync(key, endTime)

	local Timeout = false

	local function afterLoop()
		local Data
		local success, errormessage = pcall(function()
			Data = myDataStore:GetAsync(PlayerUserID,Timeout, key, endTime)
		end)
		if success then
			print("Data saved successfully")
		else
			print("There was an error!")
			warn(errormessage)
		end
	end

	coroutine.wrap(afterLoop)()

	while os.time() < endTime do
		local timeLeft = endTime - os.time()
		local minutes = math.floor(timeLeft / 60)
		local seconds = timeLeft % 60
		print(string.format("Time Left: %02d:%02d", minutes, seconds))
		wait(1)
	end

	if Timeout then
		print("Time is over!")
		myDataStore:RemoveAsync(key)
	else
		print("Timeout disabled")
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local PlayerUserID = Player.UserId   
	local success, errormessage = pcall(function()

		myDataStore:GetAsync(PlayerUserID, key, endTime)
	end)
	if success then
		print("Last Data saved successfully")
	else
		print("There was an error!")
		warn(errormessage)
	end
end)

Server-Script:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")

local success, err = pcall(function()
	myDataStore:UpdateAsync("Timeout", function(oldValue)
		return true
	end)
	print("Timeout is set True!")
end)

if not success then
	warn("Error setting timeout in data store:", err)
end

Anyways thanks for supporting me so far with this Bug and i hope we are finally able to resolve this issue together! :bug::beetle: