Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't it save, all leaderstats saves except this one?

No idea what's wrong with the fishvalue, I want it other leaderstat but not in leaderstats, I want it like hidden leaderstat which helps script but to not make it visible on leaderstats.. Here is my script

local DataStoreService = game:GetService("DataStoreService")
local CashDS = DataStoreService:GetDataStore("CashDS-")
local IbDS = DataStoreService:GetDataStore("IbDS-")
local CatchesDS = DataStoreService:GetDataStore("CatchesDS-")
local FishValueDS = DataStoreService:GetDataStore("FishValueDS-")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("IntValue",plr)
	stats.Name = "leaderstats"
	local cash = Instance.new("IntValue",stats)
	cash.Name = "Cash"
	local ib = Instance.new("IntValue",stats)
	ib.Name = "InBucket"
	local xp = Instance.new("IntValue",stats)
	xp.Name = "Catches"
	local fv = Instance.new("IntValue",plr)
	fv.Name = "FishValue"
	
	cash.Value = CashDS:GetAsync(tostring(plr.userId)) or 0
    ib.Value = IbDS:GetAsync(tostring(plr.userId)) or 0
    xp.Value = CatchesDS:GetAsync(tostring(plr.userId)) or 0
	fv.Value = FishValueDS:GetAsync(tostring(plr.userId)) or 0
	
	CashDS:SetAsync(tostring(plr.userId),cash.Value)
	IbDS:SetAsync(tostring(plr.userId), ib.Value)
	CatchesDS:SetAsync(tostring(plr.userId), xp.Value)
	FishValueDS:SetAsync(tostring(plr.userId),fv.Value)

 	cash.Changed:connect(function() 
		CashDS:SetAsync(tostring(plr.userId), cash.Value)
	end)

	ib.Changed:connect(function()
		IbDS:SetAsync(tostring(plr.userId), ib.Value)
	end)
	
	xp.Changed:connect(function()
		CatchesDS:SetAsync(tostring(plr.userId), xp.Value)
	end)
	
	fv.Changed:connect(function()
		FishValueDS:SetAsync(tostring(plr.userId), fv.Value)
	end)
end)

2 comments

0

Why are you using individual data stores for each value. You can use one giant data store and save a table instead

0

I have changed it to one but that doesnt matter

1 answer

✓ Accepted answer

Hello,

Please accept this answer.

local DataStoreService = game:GetService("DataStoreService")
local DS = DataStoreService:GetDataStore("Single_DS")

game.Players.PlayerAdded:Connect(function(plr)
    local key = tostring(plr.UserId)
    local stats = Instance.new("IntValue") -- Avoid using one line instance parenting.
    local cash = Instance.new("IntValue")
    local ib = Instance.new("IntValue")
    local xp = Instance.new("IntValue")
    local fv = Instance.new("IntValue")

    stats.Name = "leaderstats"
    cash.Name = "Cash"
    ib.Name = "InBucket"
    xp.Name = "Catches"
    fv.Name = "FishValue"

    stats.Parent = plr
    cash.Parent = stats
    ib.Parent = stats
    xp.Parent = stats
    fv.Parent = plr
        
    local data = DS:GetAsync(key)
    
    if data then
        for i, v in pairs(data) do
            print("Loaded " .. i,v )
        end
        for i,v in pairs(stats:GetChildren()) do
            v.Value = data[v.Name]
        end
        fv.Value = data[fv.Name]
    else
        for i,v in pairs(stats:GetChildren()) do
            v.Value = 0
        end
        fv.Value = 0
        data = {}
        for i,v in pairs(player.leaderstats:GetChildren()) do
            data[v.Name] = v.Value
        end
        data[fv.Name] = fv.Value
        for i, v in pairs(data) do
            print("saved " .. i,v )
        end
        DS:SetAsync(key, data)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local key = tostring(player.UserId)
    local data = {}
    for i,v in pairs(player.leaderstats:GetChildren()) do
        data[v.Name] = v.Value
    end
    data[player.FishValue.Name] = player.FishValue.Value
    for i, v in pairs(data) do
        print("saved " .. i,v )
    end
    DS:SetAsync(key, data)
end)

1 comment

1

This was resolved on discord server.

This discussion is preserved as it was. New answers, comments, and votes are closed.