#Data storage

1 messages · Page 1 of 1 (latest)

lunar belfry
#

i just test data storage so i dont add tables or like this bec the only value is coins, but can someone see my code and say to me if there is any improvements need ?

#
local DataStoreService = game:GetService("DataStoreService")
local coinsdata = DataStoreService:GetDataStore("coindata")
local runservice = game:GetService("RunService")


game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = plr:WaitForChild("leaderstats")
    local coins = leaderstats:WaitForChild("Coins")
    
    local success , savedcoins = pcall(function()
        return coinsdata:GetAsync(plr.UserId)
    end)
    
    if success and savedcoins then
        coins.Value = savedcoins
        
    else
        coins.Value = 0
        print("New Player !")
    end
    
    local time_wait = 0

    
    runservice.Heartbeat:Connect(function(deltatime)
        if plr.Parent ~= nil then
            time_wait += deltatime
            if time_wait >= 60 then
                time_wait = 0
                pcall(function()
                    coinsdata:SetAsync(plr.UserId , coins.Value)
                end)
                print("Saved !")
            end
        end
        
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local leaderstats = plr:FindFirstChild("leaderstats")
    local coins = leaderstats:FindFirstChild("Coins")
    pcall(function()
        coinsdata:SetAsync(plr.UserId , coins.Value)
    end)
end)
wild veldt
#

retries if the get or set fails and game:bindtoclose, maybe session locking, otherwise fairly traditional leaderstats/datastore script

lunar belfry
#

so u mean if player left the game and cant save ?

wild veldt
wild veldt
mental lintel
#

Also 1 heartbeat per played?

#

Plater

lunar belfry
#

i go to it he just add on local success , saveddata ....
if not success then warn("didt save ")

lunar belfry
lunar belfry
wild veldt
#

it doesn't do anything but it's still going to take up 0.0001 of a frame, every frame.

lunar belfry
mental lintel
#

Still exists

lunar belfry
wild veldt
mental lintel
#

Sure

lunar belfry
lunar belfry
# mental lintel Sure
local DataStoreService = game:GetService("DataStoreService")
local coinsdata = DataStoreService:GetDataStore("coindata")
local runservice = game:GetService("RunService")


game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = plr:WaitForChild("leaderstats")
    local coins = leaderstats:WaitForChild("Coins")
    
    local success , savedcoins = pcall(function()
        return coinsdata:GetAsync(plr.UserId)
    end)
    
    if success and savedcoins then
        coins.Value = savedcoins
        
    else
        coins.Value = 0
        print("New Player !")
    end
    
    local time_wait = 0

    
    local connect = runservice.Heartbeat:Connect(function(deltatime)
        if plr.Parent ~= nil then
            time_wait += deltatime
            if time_wait >= 60 then
                time_wait = 0
                pcall(function()
                    coinsdata:SetAsync(plr.UserId , coins.Value)
                end)
                print("Saved !")
            end
        else
            connect:Disconnect()
        end
        
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local leaderstats = plr:FindFirstChild("leaderstats")
    local coins = leaderstats:FindFirstChild("Coins")
    pcall(function()
        coinsdata:SetAsync(plr.UserId , coins.Value)
    end)
end)
```like this ?
#

there is problem here connect:Disconnect()

#

problem on connect why ?

#

here is error

#

ServerScriptService.Datastore:36: attempt to index nil with 'Disconnect'

wild veldt
#

if connect then connect:Disconnect() connect=nil end

#

etc

lunar belfry
#
if connect then
#

connect still problem

#
local connect = runservice.Heartbeat:Connect(function(deltatime)
        if plr.Parent ~= nil then
            time_wait += deltatime
            if time_wait >= 60 then
                time_wait = 0
                pcall(function()
                    coinsdata:SetAsync(plr.UserId , coins.Value)
                end)
                print("Saved !")
            end
        if plr.Parent == nil then
            connect:Disconnect()
        end
            
        end
        
    end)
#

here still connect problem idk why

#

there is orange line under connect in connect:Disconnect()

solar bay
#

inline your heartbeat event

#

to connect variable

#

wait hold on

#

do what pyrofire said

lunar belfry
#

i did there is problem

#

error *

#

i will just add else print("plr left ") to see if heartbeat stop or no

solar bay
#

the issue is the connect variable

lunar belfry
#

can u edit it ?

solar bay
#

local connect
connect = heartbeat

wild veldt
#

must be 2 lines

lunar belfry
solar bay
#

the function doesnt recognise the variable

wild veldt
#

tho i am pretty sure it should work, eh, just do both

lunar belfry
solar bay
lunar belfry
wild veldt
solar bay
#

not part of your code

wild veldt
#

works just fine for functions but apparently it's an issue there

lunar belfry
#
local DataStoreService = game:GetService("DataStoreService")
local coinsdata = DataStoreService:GetDataStore("coindata")
local runservice = game:GetService("RunService")


game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = plr:WaitForChild("leaderstats")
    local coins = leaderstats:WaitForChild("Coins")

    local success , savedcoins = pcall(function()
        return coinsdata:GetAsync(plr.UserId)
    end)

    if success and savedcoins then
        coins.Value = savedcoins

    else
        coins.Value = 0
        print("New Player !")
    end

    local time_wait = 0

    local connect
    connect = runservice.Heartbeat:Connect(function(deltatime)
        if plr.Parent ~= nil then
            time_wait += deltatime
            if time_wait >= 60 then
                time_wait = 0
                pcall(function()
                    coinsdata:SetAsync(plr.UserId , coins.Value)
                end)
                print("Saved !")
            end
        else
            connect:Disconnect()
            print("stop")
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local leaderstats = plr:FindFirstChild("leaderstats")
    local coins = leaderstats:FindFirstChild("Coins")
    pcall(function()
        coinsdata:SetAsync(plr.UserId , coins.Value)
    end)
end)
#

here is final code all thing will work correctly right ?

solar bay
#

idk does it