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

Why won't this datastore work?

Okay. In my game, i use datastores to fill out the values on the leaderboard. I am using an edited version of the Roblox U "Mad Bloxxer" scripts. (No, its not another murder game. I think we've all had enough of those) First off, here's where I first set up my datastores.

function playeradded(player)
	
	local mydata = datastore:GetAsync(player.userId) or {}
	mydata.wins = mydata.wins or 0
	mydata.BC = mydata.BC or 0
	mydata.hyperlaser = mydata.hyperlaser or 0
	
	playerdata[player.userId] = mydata
	
	
	local leaderstats = Instance.new("Model")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = mydata.wins
	wins.Parent = leaderstats
	
	local hyperlaser = Instance.new("IntValue")
	hyperlaser.Name = "Hyperlaser"
	hyperlaser.Value = mydata.hyperlaser
	hyperlaser.Parent = player
	
	local BC = Instance.new("IntValue")
	BC.Name = "BlitzCreditz"
	BC.Value = mydata.BC
	BC.Parent = leaderstats
	
	player:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats2 = player:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #stats2 do
stats2[i].Value = datastore:GetAsync(stats2[i].Name) or 0
end
	
	
end
game.Players.PlayerAdded:connect(playeradded)
for _, player in pairs(game.Players:GetPlayers()) do
	playeradded(player)
end 

Here is where I award a win to the player if that player won the game (they won the game if they still have 'MatchTag')

function awardwin(players)
    if type(players) == type({}) then

        for i = 1, #players do
	if players[i]:FindFirstChild("MatchTag") then
            if players[i]:FindFirstChild("leaderstats") then
                if  players[i].leaderstats:FindFirstChild("Wins") then 
                    local stat = players[i].leaderstats.Wins
                    stat.Value = stat.Value + 1
					local newstat = players[i].leaderstats:FindFirstChild("Wins")
						local key = "User_" .. players[i].userId
					datastore:SetAsync(key, function(oldvalue)
						local newvalue = oldvalue or 0
						newvalue = newvalue + 1
					end)
                end
            end
        end
    end
end
end

Heres my variables...



local roundtimer = 90
local intermissiontimer = 5

local datastore = game:GetService("DataStoreService"):GetDataStore("playerdata")
local serverstorage = game:GetService("ServerStorage")
local replicatedstorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local maps = serverstorage:WaitForChild("Maps")
local mapholder = game.Workspace:WaitForChild("MapHolder")
local gear = serverstorage:WaitForChild("Gear")
local statustag = replicatedstorage:WaitForChild("Status")
local timertag = replicatedstorage:WaitForChild("TimerTag")
local Winner = replicatedstorage:WaitForChild("Winner")
local resulttag = replicatedstorage:WaitForChild("result")
local event = replicatedstorage:WaitForChild("RemoteEvent")
local firstrun = true



local playerdata = {}

Finally, here's where its supposed to save the data when the player leaves

	game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() 
wait(1) 
local stats = player:FindFirstChild("leaderstats"):GetChildren()

for i = 1, #stats do
print(stats[i].Name)
datastore:SetAsync(stats[i].Name, stats[i].Value)
end
end)

Thanks for the help! Please just give me an edited script, not some vague answer with an example. Not that I want to be picky, but I am a very novice scripter, and it's very hard to learn from something so vague. If I wanted vague answers, I'd be using the Wiki ;)

1 answer

✓ Accepted answer

Datastores hold information. They don't have 'children' therefore you cannot do "mydata.Whatever"

You have to use tables.

local myData = game:GetService("DataStoreService"):GetDataStore("GameStats")
local vals = {
{"Wins",0,"IntValue"--[[Wins in the name of the value, 0 is the value,"IntValue" is the type of value]},
{"AddAnotherHere?",0,"IntValue"},
}

game.Players.PlayerAdded:connect(function(player)
	local tab = myData:GetAsync(player.userId)
	
	if not tab then -- new player set up table
		tab = {}
		local num = 0
		for i = 1,#vals do
		table.insert(tab,{vals[i][1],vals[i],2})
		num = num + 1
		
		if num % 10 == 0 then -- reduces lag
			wait()
		end 
	end
	ds:SetAsync(player.userId,tab)

	local stats = Instance.new("IntValue",player)
	stats.Name = "leaderstats"

	local num = 0
	for i = 1,#vals do
		local val = Instance.new(vals[i][3],stats)
		stats.Name = vals[i][1]
		stats.Value = vals[i][2]
		if num % 10 == 0 then -- lag reducer if you have  a lot of vals
			wait()
		end
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local newTab = {}
	local tab = myData:GetAsync(player.userId)
	
	local num = 0
	for i = 1,#tab do
		num = num + 1		
		for a,b in pairs(player.leaderstats:GetChildren()) do
			if b.Name == tab[i][1] then
				table.insert(newTab,{tab[i][1],tab[i][2],tab[i][3]})
			end
		end
		if num % 10 == 0 then
			wait()
		end
	end
	myData:SetAsync(player.userId,newTab)
end)

I'm not 100% sure that'll work, but that's the basic idea. I made it fully customizable for you so you won't have to do as much work :P.

If you have any questions about anything, message me. My username is ObscureEntity!

5 comments

0

I made it on the spot so...

0

Thank you so much! One quick question, on line 21, how does it know what ds is? I dont see it referenced anywhere else. Is that like myData?

0

Also, to increase a value on the leaderboard, can I just increase the value on the leaderboard since it

0

... will save it at the end anyway?

0

Check the message I sent you.

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