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

Unsure as to why my DataStore isn't working?

I have used a tutorial to set up a data store for my data 'Races won', however, whenever I go to try and save the data by making the player leave or waiting and then rejoin the game again, nothing happens and the data is set back to 0. I know this is a lot of code to look through, but it would be greatly appreciated if someone could see if they could figure out what the problem is.

Code:

local PlayerStatManager = {}

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

local sessionData = {}

local AUTOSAVE_INTERVAL = 60

-- function to save when player joins
local function setupPlayerData(player)
	local playerUserId = "Player_" .. player.UserId
	local data = playerData:GetAsync(playerUserId)
	if data then
		sessionData[playerUserId] = data
	else
		sessionData[playerUserId] = {["Races won"] = 0}
	end
end

game.Players.PlayerAdded:Connect(setupPlayerData)

--function to save player's data
local function savePlayerData(playerUserId)
	if sessionData[playerUserId] then
		local tries = 0
		local success
		repeat
			tries = tries + 1
			success = pcall(function()
				playerData:SetAsync(playerUserId, sessionData[playerUserId])
			end)
			if not success then wait(1) end
		until tries == 3 or success
		playerData:SetAsync(playerUserId, sessionData[playerUserId])
	end
end

--function to save player's data when they leave
local function saveOnExit(player)
	local playerUserId = "Player_" .. player.UserId
	savePlayerData(playerUserId)
end

game.Players.PlayerRemoving:Connect(saveOnExit)

--function to auto save
local function autoSave()
	while wait(AUTOSAVE_INTERVAL) do
		for playerUserId, data in pairs(sessionData) do
			savePlayerData(playerUserId)
		end		
	end
end

-- Start running "AutoSave() funciton to "PlayerAdded"
spawn(autoSave)

--Connect "setupPlayerData()" to "PlayerAddded" event
game.Players.PlayerAdded:Connect(setupPlayerData)

1 answer

Ok, lets try it!

First Step : First of all we need to know how to acces to the DataStore; the DataStore is a service so we can get it with GetService() function.

local ds = game:GetService("DataStoreService")

Second Step : We need to know how to use GetAsync() and SetAsync(). So lets take a lokk for a example I made!

--[[
    I already made a Racing Game. So I'm going to make a leaderstats to
    a race game. Basically there will be 2 script.
]]

--> Locals <--

local ds = game:GetService("DataStoreService")
local wons = ds:GetDataStore("wonsData")

--> Code! <--

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

    	local raceswon = Instance.new("IntValue", folder)
    	raceswon.Name = "Wons"
    	raceswon.Value = wons:GetAsync(plr.UserId) or 0
    	raceswon.Changed:Connect(function()
        	wons:SetAsync(plr.UserId, raceswon.Value)
    	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    	wons:SetAsync(plr.UserId, player.leaderstats.Wons.Value)
end)

local warnMessage = "The code is incorrect"
pcall(warnMessage)
--> If is incorrect will show the string on the Output

This script use the DataStore and let us store the data we want to use later. To increment the wins of the races, we have to make a touch function in another script. On the "finish" line of our track. So lets make it!

Third Step : Enjoy coding! You always have to enjoy what are you doing! Play some music while you script. Or even you can play a game to be relaxed!

Lets get into the code!

--> Locals <--

local debounce = false
local playerService = game:GetService("Players")
local localPlayer = playerService.LocalPlayer
local maxWinner = 3

--> Functions <--

local function onTouched(hit)
    	if hit.Parent.Name == "Wheel" then
        	local currency = localPlayer:WaitForChild("leaderstats").Wons

        	if not debounce then
            		print("The player "..localPlayer.Name.." won the race!")
            		currency.Value = currency.Value + 1
            		maxWinner = maxWinner + 1
		end
    	end
    	debounce = true
    	wait(3)
    	debounce = false
    	wait(3)
end

--> If statement <--

if maxWinner == 3 then
   	 print("The first, second an third place already touched!")
    	script.Parent.Touched:Disconnect(onTouched)
    	--> The next players can't touch
end

script.Parent.Touched:Connect(onTouched)
--> Put it on the finish line!

local warnMessage = "The code is incorrect!"
pcall(warnMessage)
--> FInd a error and give a warning!

That's all! We already made the leaderstats increment and other things too!

I hope my answer already solve your question. If it doesn please accept my answer, that will help me a lot!

Keep Scripting!

5 comments

0

Wow, I love your style of explaining deep into the code. You got my respect, man!

0

Never make data store calls ina changed event. You have no control over it and will reach the get/set limits quickly causing issues.

0

@kingdom, I can make a advanced DataStore, but I prefer this one. Is simple to understand and it doesn't take a lot of time!

0

I don't understand why you've used a touched event in there, I wanted it to just save the points such as editing my script, not a racing script. Thanks for trying, however.

0

.-. I saw "Wins" and I tough that was a racing game. Sorry.

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