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
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)