Hello guys, I'm trying to create stats for each player in my game, however it seems that SetAsync and UpdateAsync don't work, why? I never encountered this problem, maybe I'm doing something wrong:

This is the entire Main script:


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

function printOut(input)
	print("Level: " .. input)
end

function loadData(p, scope)
	local playerStats = datastore:GetDataStore("Stats", scope)
	local playerKnives = datastore:GetDataStore("Knives", scope)
	local playerSkins = datastore:GetDataStore("Skins", scope)
	
	local playerData = Instance.new("Model", p)
	playerData.Name = "PlayerData"
	
	local level = Instance.new("IntValue", p)
	level.Name = "Level"
	level.Value = 1
	
	local kills = Instance.new("IntValue", p)
	kills.Name = "Kills"
	kills.Value = 0
	
	local vip = Instance.new("IntValue", p)
	vip.Name = "Vip"
	vip.Value = 0
	
	local function initializeData()
		print("Initialized Data")
		pcall(function()
			playerStats:SetAsync("Level", 1)
			playerStats:SetAsync("Kills", 0)
			playerStats:SetAsync("Vip", 0)
		end)
	end
	
	pcall(function()
		if not playerStats:GetAsync("Level") then
			initializeData()
		else
			level.Value = playerStats:GetAsync("Level")
			kills.Value = playerStats:GetAsync("Kills")
			vip.Value = playerStats:GetAsync("Vip")
		end
	end)
	
	print("Loaded for player " .. p.Name .. " level: " .. level.Value .. " kills: " .. kills.Value .. " vip: " .. vip.Value)
end

function saveData(p, scope)
	local playerStats = datastore:GetDataStore("Stats", scope)
	local playerKnives = datastore:GetDataStore("Knives", scope)
	local playerSkins = datastore:GetDataStore("Skins", scope)
	
	local playerData = p.PlayerData
	pcall(function()
		print("Saving data")
		playerStats:UpdateAsync("Level", playerData.Level.Value)
		playerStats:UpdateAsync("Kills", playerData.Kills.Value)
		playerStats:UpdateAsync("Vip", playerData.Vip.Value)
	end)
	
	print("Saved for player " .. p.Name)
end


function onPlayerAdded(p)
	-- Datastore
	local scope = "cannot_show_you" .. p.UserId
	
	-- Events
	p.CharacterAdded:connect(function(character)
		onCharacterAdded(character, p, scope)
	end)
	
	p.Chatted:connect(function(msg)
		onChatted(p, msg)
	end)
end

function onPlayerRemoving(p)
	local scope = "cannot_show_you" .. p.UserId

	saveData(p, scope)
end

function onCharacterAdded(char, p, scope)
	char:WaitForChild("ForceField", 3)
	if char:FindFirstChild("ForceField") then
		char.ForceField:Destroy()
	end
	
	loadData(p, scope)
end

function onChatted(p, msg)
	-- can't show you the easter egg
end

players.PlayerAdded:connect(onPlayerAdded)
players.PlayerRemoving:connect(onPlayerRemoving)

2 comments

0

This script will error UpdateAsync requiores a function to be passed. You load data each time the character spawns? You need to rethink this script.

0

Still not working after doing the correct syntax