#Datastore Help

59 messages · Page 1 of 1 (latest)

rare dew
#

well one issue, u create a currency on plr

#

then parent it again to leaderstats

#

wah

#

cant u

#

send ur code

#

in a notepad

#

or something

#

I not like ur images

#

u didnt parent

#

leaderstats to player

#

for first

#

u need parent it to player the leaderstats

#

and the currencies

#

to leaderstats

#

since there herarchy

#

also u use

#

data saving

#

incorrectly

#

GetAssync and Set Async

#

here corrected:

#
local DataStore = game:GetService("DataStoreService"):GetDataStore("SaveName")

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = plr:WaitForChild("leaderstats")
    
    local key = "user_"..plr.UserId
    
    local savevalue1 = leaderstats:WaitForChild("Pearls")
    local savevalue2 = leaderstats:WaitForChild("Enemies Slain")
    
    local GetSaved = DataStore:GetAsync(key)
    if GetSaved then
        savevalue1.Value = GetSaved[1]
        savevalue2.Value = GetSaved[2]
    else
        local valuesForSaving = {savevalue1.Value, savevalue2.Value}
        DataStore:SetAsync(key, valuesForSaving)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local leaderstats = plr:WaitForChild("leaderstats")
    
    local savevalue1 = leaderstats:WaitForChild("Pearls")
    local savevalue2 = leaderstats:WaitForChild("Enemies Slain")
    
    DataStore:SetAsync("user_"..plr.UserId, {savevalue1.Value, savevalue2.Value})
end)
#

I dont said

#

anything

#

u need to apologize for

#

dont apologize

#

its wack

#

second argument of Instance.new is parent

#

in this example from ur code

#

the parent is plr

#

for leaderstats

#

correct

wooden forgeBOT
#

studio** You are now Level 25! **studio

rare dew
#
game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local money = Instance.new("IntValue", leaderstats)
    money.Name = "Pearls"
    money.Value = 0
    
    local kills = Instance.new("IntValue", leaderstats)
    kills.Name = "Enemies Slain"
    kills.Value = 0    
end)
#

well

#

no differenec

#

but changing things in script

#

sometimes help

#

ig

#

actually

#

why not do

#

it in one script

#
local DataStore = game:GetService("DataStoreService"):GetDataStore("SaveName")

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local savevalue1 = Instance.new("IntValue", leaderstats)
    savevalue1.Name = "Pearls"
    
    local savevalue2 = Instance.new("IntValue", leaderstats)
    savevalue2.Name = "Enemies Slain"
    
    local key = "user_"..plr.UserId
    
    local success, savedValues = pcall(function()
        return DataStore:GetAsync(key)
    end)
    
    if success and savedValues then
        savevalue1.Value = savedValues[1]
        savevalue2.Value = savedValues[2]
    else
        savevalue1.Value = 0
        savevalue2.Value = 0
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local leaderstats = plr:WaitForChild("leaderstats")
    
    local savevalue1 = leaderstats:WaitForChild("Pearls")
    local savevalue2 = leaderstats:WaitForChild("Enemies Slain")
    
    local key = "user_"..plr.UserId
    
    local success, _ = pcall(function()
        DataStore:SetAsync(key, {savevalue1.Value, savevalue2.Value})
    end)
end)
#

here just

#

one script

#

needed

#

do it in console

#

ig

#

I dont think it save in studio

#

what type of script

#

u put

#

the code

#

in

#

good

#

mhmm

#
local DataStore = game:GetService("DataStoreService"):GetDataStore("SaveName")

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local savevalue1 = Instance.new("IntValue", leaderstats)
    savevalue1.Name = "Pearls"
    
    local savevalue2 = Instance.new("IntValue", leaderstats)
    savevalue2.Name = "Enemies Slain"
    
    local key = "user_"..plr.UserId
    
    local success, savedValues = pcall(function()
        return DataStore:GetAsync(key)
    end)
    
    if success and savedValues then
        savevalue1.Value = savedValues[1]
        savevalue2.Value = savedValues[2]
    else
        savevalue1.Value = 0
        savevalue2.Value = 0
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local leaderstats = plr:WaitForChild("leaderstats")
    
    local savevalue1 = leaderstats:WaitForChild("Pearls")
    local savevalue2 = leaderstats:WaitForChild("Enemies Slain")
    
    local key = "user_"..plr.UserId
    
    local success, _ = pcall(function()
        DataStore:SetAsync(key, {savevalue1.Value, savevalue2.Value})
    end)
end)

game:BindToClose(function()
    for _, plr in pairs(game.Players:GetPlayers()) do
        local leaderstats = plr:WaitForChild("leaderstats")
        
        local savevalue1 = leaderstats:WaitForChild("Pearls")
        local savevalue2 = leaderstats:WaitForChild("Enemies Slain")
        
        local key = "user_"..plr.UserId
        
        local success, _ = pcall(function()
            DataStore:SetAsync(key, {savevalue1.Value, savevalue2.Value})
        end)
    end
end)

#

BindToClose aswell

#

try this aswell

#

idk