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

Is the datastore bugging out or is it my script?

In leaderstats are 3 intvalues (rocks wood and string) After collecting coins that increases value to the intvalues (in this case I would grab one coin that increases wood and one coin that would increase the rocks value increasing both the rocks and wood to 1000) After collecting these coins and leaving the game everything saves but one problem all values (the string included) somehow copy eachother when I never collected a coin that increases its value the string or the wood or rocks. If I were to collect two coins that increases rocks and make the rock's value 2000 but after I leave and join back it somehow copys its value and both wood and string have 2000. What is the problem?

game.Players.PlayerAdded:connect(function(p)
	local WF = Instance.new("Folder")
	WF.Name = "leaderstats"
	WF.Parent = p

	local Rocks = Instance.new("IntValue")
	Rocks.Parent = WF
	Rocks.Name = "Rocks"
	local Wood = Instance.new("IntValue")
	Wood.Parent = WF
	Wood.Name = "Wood"
	local Strings = Instance.new("IntValue")
	Strings.Parent = WF
	Strings.Name = "Strings"
end)


local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("Materials2")

players.PlayerAdded:connect(function(player)
	
	local folder = player:WaitForChild("leaderstats")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local currency1 = player.leaderstats.Rocks
	currency1.Name = "Rocks"
	currency1.Parent = player.leaderstats
	currency1.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, currency1.Value)
	
	local currency2 = player.leaderstats.Wood
	currency2.Name = "Wood"
	currency2.Parent = player.leaderstats
	currency2.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, currency2.Value)
	
	local currency3 = player.leaderstats.Strings
	currency3.Name = "Strings"
	currency3.Parent = player.leaderstats
	currency3.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, currency3.Value)
	
	currency1.Changed:connect(function()
		ds1:SetAsync(player.UserId, currency1.Value)
	end)
	currency2.Changed:connect(function()
		ds1:SetAsync(player.UserId, currency2.Value)
	end)
	
	currency3.Changed:connect(function()
		ds1:SetAsync(player.UserId, currency3.Value)
	end)
end)

1 answer

✓ Accepted answer

You are saving it to the same datastore, you are trying to put the user into one data store 3 times which is not possible. You need to create a new data store for each value, such as, ds1, ds2, ds3.

Here is the fixed code:

game.Players.PlayerAdded:connect(function(p)
    local WF = Instance.new("Folder")
    WF.Name = "leaderstats"
    WF.Parent = p

    local Rocks = Instance.new("IntValue")
    Rocks.Parent = WF
    Rocks.Name = "Rocks"
    local Wood = Instance.new("IntValue")
    Wood.Parent = WF
    Wood.Name = "Wood"
    local Strings = Instance.new("IntValue")
    Strings.Parent = WF
    Strings.Name = "Strings"
end)


local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("Materials1")
local ds2 = datastore:GetDataStore("Materials2")
local ds3 = datastore:GetDataStore("Materials3")

players.PlayerAdded:connect(function(player)

    local folder = player:WaitForChild("leaderstats")
    folder.Name = "leaderstats"
    folder.Parent = player

    local currency1 = player.leaderstats.Rocks
    currency1.Name = "Rocks"
    currency1.Parent = player.leaderstats
    currency1.Value = ds1:GetAsync(player.UserId) or 0
    ds1:SetAsync(player.UserId, currency1.Value)

    local currency2 = player.leaderstats.Wood
    currency2.Name = "Wood"
    currency2.Parent = player.leaderstats
    currency2.Value = ds1:GetAsync(player.UserId) or 0
    ds2:SetAsync(player.UserId, currency2.Value)

    local currency3 = player.leaderstats.Strings
    currency3.Name = "Strings"
    currency3.Parent = player.leaderstats
    currency3.Value = ds1:GetAsync(player.UserId) or 0
    ds3:SetAsync(player.UserId, currency3.Value)

    currency1.Changed:connect(function()
        ds1:SetAsync(player.UserId, currency1.Value)
    end)
    currency2.Changed:connect(function()
        ds2:SetAsync(player.UserId, currency2.Value)
    end)

    currency3.Changed:connect(function()
        ds3:SetAsync(player.UserId, currency3.Value)
    end)
end)

Be sure to mark as the answer if this helped.

2 comments

1

well it would be better to save it as a table

1

Yes but in his case this is what he was asking. He needed help with his code erroring, not a whole new code.

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