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

How do i reset players leaderstats? I just seen this and was scared.

Someone hacked leaderstats and you can see it on global leaderboard. I should get rid of it, and maybe someone can give me good anti exploit script. Hacked leaderboard image: [https://imgur.com/JU0wH95] Default leaderboard image [https://imgur.com/0fUI4Ao]

Here you can see leaderboard script

local sg = script.Parent --Surface GUI
local sample = script:WaitForChild("Sample") --Our Sample frame
local sf = sg:WaitForChild("ScrollingFrame") --The scrolling frame
local ui = sf:WaitForChild("UI") --The UI list layout

local dataStoreService = game:GetService("DataStoreService")
--The data store service
local dataStore = dataStoreService:GetOrderedDataStore("Leaderboard")
--Get the data store with key "Leaderboard"
wait(5)
while true do
	for i,plr in pairs(game.Players:GetChildren()) do--Loop through players
		if plr.UserId>0 then--Prevent errors
			local ps = game:GetService("PointsService")--PointsService
			local w = plr.leaderstats.Tokens.Value --Get point balance CHANGE THE HERE
			if w then
				pcall(function()
				--Wrap in a pcall so if Roblox is down, it won't error and break.
					dataStore:UpdateAsync(plr.UserId,function(oldVal)
				        --Set new value
						return tonumber(w)
					end)
				end)
			end
		end
	end    
	local smallestFirst = false--false = 2 before 1, true = 1 before 2
    local numberToShow = 100--Any number between 1-100, how many will be shown
    local minValue = 1--Any numbers lower than this will be excluded
    local maxValue = 10^30--(10^30), any numbers higher than this will be excluded, 9000000000
    local pages = dataStore:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue)
    --Get data
    local top = pages:GetCurrentPage()--Get the first page
	local data = {}--Store new data
	for a,b in ipairs(top) do--Loop through data
		local userid = b.key--User id
		local points = b.value--Points
		local username = "[Failed To Load]"--If it fails, we let them know
		local s,e = pcall(function()
		 username = game.Players:GetNameFromUserIdAsync(userid)--Get username
		end)
		if not s then--Something went wrong
		   warn("Error getting name for "..userid..". Error: "..e)
		end
		local image = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
		--Make a image of them
		table.insert(data,{username,points,image})--Put new data in new table
	end
	ui.Parent = script
	sf:ClearAllChildren()--Remove old frames
	ui.Parent = sf
	for number,d in pairs(data) do--Loop through our new data
		local name = d[1]
		local val = d[2]
		local image = d[3]
		local color = Color3.new(81, 69, 14)--Default color
		if number == 1 then
			color = Color3.fromRGB(122, 236, 228)--1st place color
		elseif number == 2 then
			color = Color3.fromRGB(209, 169, 12)--2nd place color
		elseif number == 3 then
			color = Color3.fromRGB(191, 194, 215)--3rd place color
		end
		local new = sample:Clone()--Make a clone of the sample frame
		new.Name = name--Set name for better recognition and debugging
        new.LayoutOrder = number--UIListLayout uses this to sort in the correct order
		new.Image.Image = image--Set the image
		new.Image.Place.Text = number--Set the place
		new.Image.Place.TextColor3 = color--Set the place color (Gold = 1st)
		new.PName.Text = name--Set the username
		new.Value.Text = val--Set the amount of points
		new.Value.TextColor3 = color--Set the place color (Gold = 1st)
		new.PName.TextColor3 = color--Set the place color (Gold = 1st)
		new.Parent = sf--Parent to scrolling frame
	end
	wait()
	sf.CanvasSize = UDim2.new(0,0,0,ui.AbsoluteContentSize.Y)
	--Give enough room for the frames to sit in
	wait(120)
end

Here you can see data store script

game.Players.PlayerAdded:connect(function(p)
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"
	stats.Parent = p
	
	local money = Instance.new("IntValue")
	money.Name = "Tokens" 
	money.Value = 0
	money.Parent = stats
end)

local AUTO_SAVE = true			-- Make true to enable auto saving
local TIME_BETWEEN_SAVES = 60	-- In seconds (WARNING): Do not put this lower than 60 seconds
local PRINT_OUTPUT = false		-- Will print saves and loads in the output
local SAFE_SAVE = true			-- Upon server shutdown, holds server open to save all data

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local leaderboardData = dataStoreService:GetDataStore("LeaderStats")

local function Print(message)
	if PRINT_OUTPUT then print(message) end
end

local function SaveData(player)
	if player.userId < 0 then return end
	player:WaitForChild("leaderstats")
	wait()
	local leaderstats = {}
	for i, stat in pairs(player.leaderstats:GetChildren()) do
		table.insert(leaderstats, {stat.Name, stat.Value})
	end
	leaderboardData:SetAsync(player.userId, leaderstats)
	Print("Saved "..player.Name.."'s data")
end

local function LoadData(player)
	if player.userId < 0 then return end
	player:WaitForChild("leaderstats")
	wait()
	local leaderboardStats = leaderboardData:GetAsync(player.userId)
	for i, stat in pairs(leaderboardStats) do
		local currentStat = player.leaderstats:FindFirstChild(stat[1])
		if not currentStat then return end
		currentStat.Value = stat[2]
	end
	Print("Loaded "..player.Name.."'s data")
end

players.PlayerAdded:connect(LoadData)
players.PlayerRemoving:connect(SaveData)

if SAFE_SAVE then
	game.OnClose = function()
		for i, player in pairs(players:GetChildren()) do
			SaveData(player)
		end
		wait(1)
	end
end

while AUTO_SAVE do
	wait(TIME_BETWEEN_SAVES)
	for i, player in pairs(players:GetChildren()) do
		SaveData(player)
	end
end

1 comment

0

This is not a good place to get your script done. Do it yourself, if there is any problems then come here.

1 answer

I think what your trying to do is stop exploiters in game. To do that you should use a remote event and remote function this means that you can tell the client to the server basically, anything that I malicious the server will not accept it.

Alvinblox made a great video about how to stop exploiters in game:

Link: https://www.youtube.com/watch?v=wic-N4JiFss&t=639s

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