Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

OnUpdate callback being called every time a server starts?

Asked by 6 years ago

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

0
Where is the OldVoids? thesit123 509 — 6y

1 answer

Log in to vote
0
Answered by
thesit123 509 Moderation Voter
6 years ago

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)
0
Sorry my bad for not commenting on the script for what it does. thesit123 509 — 6y
Ad

Answer this question