#I want to improve my script

1 messages · Page 1 of 1 (latest)

pure trench
#

this is my data store script but i want to make it better and for sure when i wand to add more data like points ,tools, not only coins , i dont want to add many lines , and make it (readable)

mint mural
pure trench
#

there is no way i can just collect all data and make it 1 thing

#

like how to add a table that have all data ? @mint mural

mint mural
#

numbers are easy. tables like inventory data is more involved. you maybe haven't reached that point yet, but you will 🙂

pure trench
#

oh u mean like tools cant be inside table coins or leaderstats ?

mint mural
pure trench
#

oh ok

mint mural
#

it's a bit like that

pure trench
#

so like is my data store good or need some fixes ?

mint mural
pure trench
#

ty 🙂

#

but i want to improve it

#

all videos i see just do my way i do it so how i make it better ?

mint mural
# pure trench but i want to improve it

you won't be able to properly improve it, or understand how, short of me telling you exactly what code to put, because you don't yet understand how or why this isn't enough. i can tell you it is (mostly) enough and any forseeable problems are very niche.

mint mural
mint mural
#

the only forseeable problem at this level is if roblox datastore servers crash and tbh you probably don't need to care about that

pure trench
mint mural
#

that's a very hard problem. your code is good enough 👍

pure trench
mint mural
pure trench
#

ok cya

mint mural
pure trench
mint mural
mint mural
pure trench
#

ok make sense

mint mural
#

just dont take the code at its face value, its like copying code out of a 20 year old book, dont expect it to work first try without your input salute

#

from tutorials or ai etc that is

pure trench
#

oh why people add also local success , errormassege = pcall ?

mint mural
#

everything is in the docs

pure trench
#

i know pcall to protect from any mistake like if there was weak wifi , or game is laging alot but i know i just use success , saved coins not success , error

mint mural
#

look up every keyword, you just never know what you might learn salute

pure trench
#

ohh people use success and error bec if there was any error , so can i put it inside while plr.parent do and add it ? @mint mural

#
local datastoreservice = game:GetService("DataStoreService")
local coinsdata = datastoreservice:GetDataStore("coinsdata")
game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = plr:WaitForChild("leaderstats")
    local coins = leaderstats:FindFirstChild("Coins")
    local success , savedcoins = pcall(function()
        return coinsdata:GetAsync(plr.UserId)
    end)
    
    if success and savedcoins then
        coins.Value = savedcoins
    else
        coins.Value = 0
    end
    
    while plr.Parent do 
        task.wait(60)
        local success , error_massege = pcall(function()
            return coinsdata:SetAsync(plr.UserId , coins.Value)
        end)
        if not success then
            print(error_massege)
        end
        pcall(function()
            coinsdata:SetAsync(plr.UserId , coins.Value)
        end)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr_leave)
    local leaderstats = plr_leave:WaitForChild("leaderstats")
    local coins = leaderstats:FindFirstChild("Coins")

    pcall(function()
        coinsdata:SetAsync(plr_leave.UserId , coins.Value)
    end)
end)

#

like this @mint mural i added while plr.parent

pure trench
mint mural
#

idk about calling setasync twice in a row but

#

you'll get there 🙂