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

HELP ME FIX THIS SCRIPT? Leaderboard Glitching Out In My Game

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("LeaderStats")

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

	local plrkey = "id_"..plr.userId
	
	local Kills = Instance.new("IntValue",leaderstats)
	Kills.Name = "Kills"

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

	local EXP = Instance.new("IntValue",leaderstats)
	EXP.Name = "EXP"
	
	local Rank = Instance.new("IntValue",leaderstats)
	Rank.Name = "Rank"

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

	local data
	local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(plr.UserId.."-KILLS")
	
end)

if success then
	
	Kills.Value = data
	
else
		print("There Was An Error Whilst Getting Your Data")
		warn(errormessage)
	end
	
	local data
	local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(plr.UserId.."-KILLS")
	
end)

if success then
	
	XP.Value = data
	
else
		print("There Was An Error Whilst Getting Your Data")
		warn(errormessage)
	end
		
	local data
	local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(plr.UserId.."-XP")
	
end)

if success then
	
	EXP.Value = data
	
else
		print("There Was An Error Whilst Getting Your Data")
		warn(errormessage)
	end
		
	local data
	local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(plr.UserId.."-EXP")
	
end)

if success then
	
	Rank.Value = data
	
else
		print("There Was An Error Whilst Getting Your Data")
		warn(errormessage)
	end
		
	local data
	local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(plr.UserId.."-RANK")
	
end)

if success then
	
	CR.Value = data
	
else
		print("There Was An Error Whilst Getting Your Data")
		warn(errormessage)
	end
	
	local data
	local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(plr.UserId.."-CR")
	
end)

	plr.CharacterAdded:Connect(function(char)
		local humanoid = char:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Died:Connect(function()
				for i, Chill in pairs(humanoid:GetChildren()) do
					if Chill:IsA("ObjectValue") and Chill.Value and Chill.Value:IsA("Player") then
						local Killer = Chill.Value
						Killer.leaderstats.Kills.Value = Killer.leaderstats.Kills.Value + 1
						Killer.leaderstats.XP.Value = Killer.leaderstats.XP.Value + 100
						while humanoid.Running:Wait() do
							Killer.leaderstats.XP.Value = Killer.leaderstats.XP.Value + 100
						end
					end
				end
			end)
		end
	end)
	while wait() do
		EXP.Value = plr.leaderstats.Rank.Value*1000+1000
		if XP.Value >= EXP.Value then
			XP.Value = XP.Value - EXP.Value
			Rank.Value = plr.leaderstats.Rank.Value + 1
			CR.Value = CR.Value + 200+5*Rank.Value
		end
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-KILLS",player.leaderstats.Kills.Value)
	end)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-XP",player.leaderstats.XP.Value)
		
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-EXP",player.leaderstats.EXP.Value)
			
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-RANK",player.leaderstats.Rank.Value)
				
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-CR",player.leaderstats.CR.Value)	
					
	if success then
	
		print("Player Data Successfully Saved!")
	else
		print("There Was An Error When Saving Data")
		warn(errormessage)
	end	
	end)			
	end)
	end)
	end)
	end)

This Script is Glitching Out For Me I Made It Myself and i don't know how to fix it do you know how to fix it. when ever i use it it says EXP -9,220..... XP is blicking -9,220..... and Rank is -9,220..... Can Anyone Help Me Fix This Script Please?

1 comment

0

I wouldn't even bother fixing that, it looks unnecessarily long, i am ssuming you just want leaderstats that save, so I will provide an answer

2 answers

Alright so I would do it like this, firstly insert a folder in server storage, and insert 3 int values(the int values should have the names of the stats)inside the folder, name the folder "leaderstats" then put this script in ServerScriptService:

local datstorage=game:GetService("DataStoreService")
local savedCash=datstorage:GetDataStore("Cash")
local savedKills=datstorage:GetDataStore("Kills")
local savedRank=datstorage:GetDataStore("Rank")

game.Players.PlayerAdded:Connect(function(player)
local leaderstats=game.ServerStorage.leaderstats:Clone()
local success,errormessage pcall(function()
leaderstats.Cash.Value=savedCash:GetAsync(player.UserId)
leaderstats.Kills.Value=savedKills:GetAsync(player.UserId)
leaderstats.Rank.Value=savedRank:GetAsync(player.UserId)
if success then 
print("success") 
else print(errormessage)
end
end)

leaderstats.Parent=player
end)

game.Players.PlayerRemoving:Connect(function(player)
local leaderstats=player.leaderstats
local success,errormessage pcall(function()
savedCash:SetAsync(player.UserId,leaderstats.Cash.Value)
savedKills:SetAsync(player.UserId,leaderstats.Kills.Value)
savedRank:SetAsync(player.UserId,leaderstats.Rank.Value)
if success then 
print("success") 
else print(errormessage)
end
end)
end)

3 comments

0

Also make sure Api access is turned on

0

only problem with this script/ the diffence that i want is that i want the ablilty for something like a EXP witch allows once you get like 10 kills or something like for each kill to = 100 XP and that will go up in time so Once you hit like 1000 XP you hit Level 1

0

that is unrelated to datastore though, do that in a different script

	local DataStoreService = game:GetService("DataStoreService")
	 
	local myDataStore = DataStoreService:GetDataStore("leaderstats")
	 
	game.Players.PlayerAdded:Connect(function(plr)
	    local leaderstats = Instance.new("Folder",plr)
	    leaderstats.Name = "leaderstats"
	    leaderstats.Parent = plr	 
	    local plrkey = "id_"..plr.userId
	 
	    local Kills = Instance.new("IntValue",leaderstats)
	    Kills.Name = "Kills"
	 
	    local XP = Instance.new("IntValue",leaderstats)
	    XP.Name = "XP"
	 
	    local EXP = Instance.new("IntValue",leaderstats)
	    EXP.Name = "EXP"
	 
	    local Rank = Instance.new("IntValue",leaderstats)
	    Rank.Name = "Rank"
	 
	    local CR = Instance.new("IntValue",leaderstats)
	    CR.Name = "CR"
	 
	    local data
	    local success, errormessage = pcall(function()
	    data = myDataStore:GetAsync(plr.UserId.."-KILLS")
		data = myDataStore:GetAsync(plr.UserId.."-XP")
	    data = myDataStore:GetAsync(plr.UserId.."-EXP")		
	    data = myDataStore:GetAsync(plr.UserId.."-RANK")		
		data = myDataStore:GetAsync(plr.UserId.."-CR") 
	end)
	 
	if success then
	 
	    Kills.Value = data
		XP.Value = data
		EXP.Value = data
		Rank.Value = data
		CR.Value = data
		
	else
	        print("There Was An Error Whilst Getting Your Data")
	        warn(errormessage)
	    end
	    plr.CharacterAdded:Connect(function(char)
	        local humanoid = char:FindFirstChild("Humanoid")
	        if humanoid then
	            humanoid.Died:Connect(function()
	                for i, Chill in pairs(humanoid:GetChildren()) do
	                    if Chill:IsA("ObjectValue") and Chill.Value and Chill.Value:IsA("Player") then
	                        local Killer = Chill.Value
	                        Killer.leaderstats.Kills.Value = Killer.leaderstats.Kills.Value + 1
	                        Killer.leaderstats.XP.Value = Killer.leaderstats.XP.Value + 100
	                        while humanoid.Running:Wait() do
	                            Killer.leaderstats.XP.Value = Killer.leaderstats.XP.Value + 100
	                        end
	                    end
	                end
	            end)
	        end
	    end)
	    while wait() do
	        EXP.Value = plr.leaderstats.Rank.Value*1000+1000
	        if XP.Value >= EXP.Value then
	            XP.Value = XP.Value - EXP.Value
	            Rank.Value = plr.leaderstats.Rank.Value + 1
	            CR.Value = CR.Value + 200+5*Rank.Value
	        end
	    end
	end)

game.Players.PlayerRemoving:Connect(function(player)
	 local leaderstats = player.leaderstats
	    local success, errormessage = pcall(function()
			myDataStore:SetAsync(player.UserId.."-KILLS",player.leaderstats.Kills.Value)
	        myDataStore:SetAsync(player.UserId.."-XP",player.leaderstats.XP.Value)
	        myDataStore:SetAsync(player.UserId.."-EXP",player.leaderstats.EXP.Value)
	        myDataStore:SetAsync(player.UserId.."-RANK",player.leaderstats.Rank.Value)
	        myDataStore:SetAsync(player.UserId.."-CR",player.leaderstats.CR.Value) 	   
	 end)
	 
	    if success then
	 
	        print("Player Data Successfully Saved!")
	    else
	        print("There Was An Error When Saving Data")
	        warn(errormessage)
	    end
	    end)

IF anyone is wondering i have fixed my script from some help of the answer above that script made me base it off of this one THANK YOU SIR

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