Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

OnUpdate callback being called every time a server starts?

Hello scripters, I was messing around with the OnUpdate function in DataStores, and came across this weird issue that I'm not sure how to solve. I asked around and it seemed like nobody knew why it was happening, so I decided to ask you guys. Alright, here's the issue.

I have this code

local DataStore = game:GetService("DataStoreService"):GetDataStore("ds")
local connection = DataStore:OnUpdate("key", print)
game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg)
		if msg == "enable" then
			DataStore:SetAsync("key","xd")
			connection:Disconnect()
		end
	end)
end)

By looking at it you can see what it's supposed to do. When a player chats 'enable', it sets the value of "key" to "xd" in the datastore. Then it will print "xd" because I used print in OnUpdate. When I join the game and chat 'enable', 'xd' is printed in the server log. However, when I rejoin, it is printed again, without me even chatting 'enable'. This happens every time I rejoin after chatting 'enable'. Does anyone know what's causing this and how I could fix it? Thanks,

NewVoids

1 comment

0

Where is the OldVoids?

1 answer

Try getting the async with :GetAsync() right after player joins and if there is a value on key, then Disconnect()? Here is the link to Roblox Wiki: Link

local DataStore = game:GetService("DataStoreService"):GetDataStore("ds")
local connection = DataStore:OnUpdate("key", print)
game.Players.PlayerAdded:connect(function(player)
	local gds = DataStore:GetAsync(player)
	if gds == "xd" then
		connection:Disconnect()
	else
		player.Chatted:connect(function(msg)
			if msg == "enable" then
				DataStore:SetAsync("key","xd")
				connection:Disconnect()
			end
		end)
	end
end)

1 comment

0

Sorry my bad for not commenting on the script for what it does.

This discussion is preserved as it was. New answers, comments, and votes are closed.