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

My Data Store script isn't working and there are no errors. Any help?

So i'm making a data store for my game but it's not working. Here is my script.

local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
local playersleft = 0

game.Players.PlayerAdded:Connect(function(player)
	playersleft = playersleft + 1
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	local exp = Instance.new("IntValue",leaderstats)
	exp.Name = "Exp"
	local maxexp = Instance.new("IntValue",leaderstats)
	maxexp.Name = "MaxExp"
	maxexp.Value = 100
	local level = Instance.new("IntValue",leaderstats)
	level.Name = "Level"
	local toonys = Instance.new("IntValue",leaderstats)
	toonys.Name = "Toonys"
	if level.Value == level.Value + 1 then
		maxexp.Value = maxexp.Value * 2
	end
	local exp_data
	local level_data
	local toony_data


	pcall(function()
		exp_data = datastore:GetAsync(player.UserId.." Exp")
		level_data = datastore:GetAsync(player.UserId.." Level")
		toony_data = datastore:GetAsync(player.UserId.."Toonys")
	end)
	if toony_data ~= nil and level_data ~= nil and exp_data ~= nil then
		toonys.Value = toony_data
		exp.Value = exp_data
		level.Value = level_data
	else
		toonys.Value = 5
		level.Value = 1
		exp.Value = 0
	end	
end)

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		playersleft = playersleft - 1
		datastore:SetAsync(player.UserId.." Exp",player.leaderstats.Exp.Value)
		datastore:SetAsync(player.UserId.." Level",player.leaderstats.Level.Value)
		datastore:SetAsync(player.UserId.." Toonys",player.leaderstats.Toonys.Value)
		print("Saved!")
	end)
end)

1 answer

✓ Accepted answer

Oh I see what you have done. You have set only one data store for 3 values. This just overwrites the before variables, so in the end, you only save the data: Toonys.

Here is a solution:

local datastore = game:GetService("DataStoreService")
local  Exp = datastore:GetDataStore("Exp")
local  Toony = datastore:GetDataStore("Toony")
local  Level = datastore:GetDataStore("Level ")
local playersleft = 0


game.Players.PlayerAdded:Connect(function(player)

    playersleft = playersleft + 1

    local leaderstats = Instance.new("Folder",player)

    leaderstats.Name = "leaderstats"

    local exp = Instance.new("IntValue",leaderstats)

    exp.Name = "Exp"

    local maxexp = Instance.new("IntValue",leaderstats)

    maxexp.Name = "MaxExp"

    maxexp.Value = 100

    local level = Instance.new("IntValue",leaderstats)

    level.Name = "Level"

    local toonys = Instance.new("IntValue",leaderstats)

    toonys.Name = "Toonys"

    if level.Value == level.Value + 1 then

        maxexp.Value = maxexp.Value * 2

    end

    local exp_data

    local level_data

    local toony_data

 
    pcall(function()

        exp_data = Exp:GetAsync(player.UserId.." Exp")

        level_data = Level:GetAsync(player.UserId.." Level")

        toony_data = Toony:GetAsync(player.UserId.."Toonys")

    end)

    if toony_data ~= nil and level_data ~= nil and exp_data ~= nil then

        toonys.Value = toony_data

        exp.Value = exp_data

        level.Value = level_data

    else

        toonys.Value = 5

        level.Value = 1

        exp.Value = 0

    end

end)

game.Players.PlayerRemoving:Connect(function(player)

    pcall(function()

        playersleft = playersleft - 1

        Exp:SetAsync(player.UserId.." Exp",player.leaderstats.Exp.Value)

        Level:SetAsync(player.UserId.." Level",player.leaderstats.Level.Value)

        Toony:SetAsync(player.UserId.." Toonys",player.leaderstats.Toonys.Value)

        print("Saved!")

    end)

end)

Hope this helped!

1 comment

0

That makes sense! and thanks it worked!

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