#Leaderstats

36 messages · Page 1 of 1 (latest)

near obsidian
#


local DataStoreService = game:GetService("DataStoreService")
local revolverAmmoDataStore = DataStoreService:GetDataStore("RevolverAmmoDataStore")

game.Players.PlayerAdded:Connect(function(player)
    -- Create the Leaderstats folder
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    -- Create the RevolverAmmo (will be saved)
    local revolverAmmo = Instance.new("IntValue")
    revolverAmmo.Name = "RevolverAmmo"
    revolverAmmo.Parent = leaderstats

    -- Attempt to load saved data for the player
    local success, data = pcall(function()
        return revolverAmmoDataStore:GetAsync(tostring(player.UserId)) -- Using GetAsync correctly
    end)

    if success and data then
        revolverAmmo.Value = data -- Assign the loaded value from the DataStore
    else
        revolverAmmo.Value = 0 -- If no saved data, start with 0
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    -- Check if RevolverAmmo is in the leaderstats folder
    local revolverAmmo = player.leaderstats and player.leaderstats:FindFirstChild("RevolverAmmo")

    if revolverAmmo then
        -- Save the RevolverAmmo data when the player leaves
        local success, errorMessage = pcall(function()
            revolverAmmoDataStore:SetAsync(tostring(player.UserId), revolverAmmo.Value) -- Saving the RevolverAmmo value to the DataStore
        end)

        if not success then
            warn("Error saving data: " .. errorMessage)
        end
    else
        warn("RevolverAmmo not found for player " .. player.Name)
    end
end)

Why does this script only save the RevolverAmmo when I win ammo and not when I lose as well?

silk bluff
near obsidian
#

Who I solve it?

stark elmBOT
#

studio** You are now Level 2! **studio

silk bluff
#

Make sure that the server changes all values similar to these, it's also generally more secure

near obsidian
silk bluff
near obsidian
near obsidian
lusty fjord
near obsidian
#

no, when I gain ammo the leaderstats saves, but when I lose it doesn't save

silk bluff
lusty fjord
#

wait a minute is it like, only a local script thing?

silk bluff
stark elmBOT
#

studio** You are now Level 6! **studio

near obsidian
lusty fjord
#

i have a feeling that he needs like a seperate script thats a server script and a remote event

#

i dont know tho

#

im new

near obsidian
#

but I never needed this to create leaderstats with data store

lusty fjord
# near obsidian I am making this

i dont know if this will help but,
i feel like you should first make a script that makes a leaderstats then a localscript that waits when the ammo picked up the it will fire to the remote event to the serverscript that basically does all the shit on the leaderstats

#

but its just an opinion im still pretty dumb on roblox studio

#

but since i dont know what shit ur making eh 🤷‍♂️

near obsidian
#

I used this leaderstats model:

#

(wait a minute)

#

local Salvar = game:GetService("DataStoreService"):GetDataStore("SaveData03")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player

    local Din = Instance.new("IntValue")
    Din.Name = 'Dinheiro'
    Din.Parent = leaderstats

    local Pat = Instance.new("StringValue")
    Pat.Name = 'Patente'
    Pat.Parent = leaderstats

    local Div = Instance.new("StringValue")
    Div.Name = 'Divisao'
    Div.Parent = leaderstats

    local Data = Salvar:GetAsync(player.UserId)

    if Data then
        print("Encontrei Data")
        Din.Value = Data[1]
        Pat.Value = Data[2]
        Div.Value = Data[3]
    else
        print("Sem data vamos criar uma para ti")
        Din.Value = 500
        Pat.Value = 'Civil'
        Div.Value = 'N/A'
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    Salvar:SetAsync(player.UserId, {player.leaderstats.Dinheiro.Value, player.leaderstats.Patente.Value, player.leaderstats.Divisao.Value})
    print("Salvo com sucesso")
end)

#

but dont work

#

now no more

lusty fjord
near obsidian
#

Nop, it prints everything correctly, but the error is the same

near obsidian
#

I already solve it thx for the help @silk bluff and @lusty fjord

lusty fjord