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

I need help. My data store wont save the data. Why?

It wont save. Any ideas?

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Leaderstats")

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder",plr)
    leaderstats.Name = "leaderstats"

    local level = Instance.new("IntValue", leaderstats)
    level.Name = "Level"
    level.Value = 1
    pcall(function()
        local function save(plr)
            local leaderstats = plr:WaitForChild("leaderstats")
            local level = leaderstats:WaitForChild("Level")

            pcall(function()
                dataStore:SetAsync(plr.UserId.. "_Level",level.Value)
            end)
        end

        game.Players.PlayerRemoving:Connect(function(plr)
            save(plr)
        end)

        game:BindToClose(function() -- runs when game closes/shutdowns
            for _, plr in pairs(game.Players:GetPlayers()) do
                save(plr)
            end
        end)

        game.Players.PlayerRemoving:Connect(function(plr)
            local leaderstats = plr:WaitForChild("leaderstats")
            local level = leaderstats:WaitForChild("Level")

            pcall(function()
                dataStore:SetAsync(plr.UserId.. "Level",level.Value)

            end)
        end)
    end)
end)

2 answers

✓ Accepted answer

The script you made, made no sense (sorry). I'm not sure why you put the PlayerRemoving event inside the PlayerAdded event along with the BindToClose function. But good thing you knew about :BindToClose()

You simply had to put the event PlayerRemoving outside the PlayerAdded thing and the BindToClose as well. And try organizing your script better by putting the save function outside the PlayerAdded event. You can ignore the pcall function if you're not using it. If you want to tell the player something didn't save, then re-add it and make use of it. Otherwise just keep it away...

I hope you learned something.

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Leaderstats")

local function save(plr)
	local leaderstats = plr:WaitForChild("leaderstats")
	local level = leaderstats:WaitForChild("Level")
	dataStore:SetAsync(plr.UserId.. "_Level",level.Value)
end

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder",plr)
    leaderstats.Name = "leaderstats"

    local level = Instance.new("IntValue", leaderstats)
    level.Name = "Level"
    level.Value = 1
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	save(plr)
end)

game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
		save(plr)
	end
end)

2 comments

0

what does the _, mean in the for loop on line 25?

0

It is the indexation, most people call it "i" or "_"

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Leaderstats")

local function save(plr)
    local leaderstats = plr.leaderstats
    local level = leaderstats.Level
    pcall(function()
	dataStore:SetAsync(plr.UserId.. "_Level",level.Value)
    end)
end

game.Players.PlayerAdded:Connect(function(plr)
    local data
    local suc, err = pcall(function()
	data = dataStore:GetAsync(plr.UserId.. "_Level")
    end)
    if not suc or data == nil then
	data = 1
    end
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"


    local level = Instance.new("IntValue")
    level.Name = "Level"
    level.Value = data
    level.Parent = leaderstats
    leaderstats.Parent = plr
    end)
end)

game.Players.PlayerRemoving:Connect(save)

game:BindToClose(function()
    for _, plr in pairs(game.Players:GetPlayers()) do
        save(plr)
    end
end)

sorry for bad indenting this just addon to other dude

1 comment

0

Still a little confused, it wont work still.

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