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

I tried adding a datastore to the leaderboard and it doenst work?

i get no error message or script errors?

local datastoreservice = game:GetService("DataStoreService")
local mydata = datastoreservice:GetDataStore("mydata")

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

local PlayerUserid = "Player_"..player.UserId


local success, errormessage = pcall(function()
	data = mydata:GetaSync(PlayerUserid)
end)

if success then
	Credits.Value = data
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local PlayerUserid = "Player_"..player.UserId
local data = player.leaderstats.Credits.Value
local success, errormessage = pcall(function()
	mydata:SetAsync(PlayerUserid, data)
end)
if success then
	print("Good")
else
	print("Bad")
	warn(errormessage)
end
end)

1 answer

✓ Accepted answer

Improvements

Use :GetService() to retrieve the Players service

Use :WaitForChild() to make sure an instance exists before using it

Set the Parent of a new instance on a separate line, it is more efficient for the API

Issues

"data" is not predefined before the pcall

Lua is case sensitive, so "GetaSync" would not be called correctly, it must be GetAsync

Revised Server Script

local Players = game:GetService("Players")
local datastoreservice = game:GetService("DataStoreService")
local mydata = datastoreservice:GetDataStore("mydata")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local HatPoints = Instance.new("IntValue")
	HatPoints.Name = "HatPoints"
	HatPoints.Parent = leaderstats
	
	local Credits = Instance.new("IntValue")
	Credits.Name = "Credits"
	Credits.Parent = leaderstats
	
	local PlayerUserid = "Player_"..player.UserId
	
	local data = nil
	local success, errormessage = pcall(function()
		data = mydata:GetAsync(PlayerUserid)
	end)
	
	if success then
		Credits.Value = data
	else
		warn(errormessage)
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local PlayerUserid = "Player_"..player.UserId
	local data = player:WaitForChild("leaderstats"):WaitForChild("Credits").Value
	
	local success, errormessage = pcall(function()
		mydata:SetAsync(PlayerUserid, data)
	end)
	
	if not success then
		warn(errormessage)
	end
end)

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