Automatic Points system based on time spent in game

I am aiming to create a system that adds points to a player every 3 minutes and then saves those points onto a datastore. I have a version that works but that version breaks things such as the rank leaderboard and it gives points to everyone instead of only people in a group that have a certain rank.

I will show the current code that I have bellow:
In server script service, adds the points to the player

function onClicked()
local p = game.Players:GetChildren()
for i = 1, #p do
p[i].Character:MoveTo(Vector3.new(220.583, 17.119, -124.123))
end
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked)

In server script service, is responsible for the database and auto saves

local DataStoreService = game:GetService("DataStoreService")

local PointSave = DataStoreService:GetDataStore("PointSave")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = PointSave:GetAsync(player.UserId)
	end)
	
	if success then
		points.Value = data
	else
		print("There was an error whilst loading your points.")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		PointSave:SetAsync(player.UserId,player.leaderstats.Points.Value)
	end)
		
		if success then
			print("Points saved!")
		else
			print("Points were not saved due to an error.")
			warn(errormessage)
		end
		
end)

In starter player scripts, not exactly sure what this does, I think it fires the event to add points

local groupID = 5243394
local rank = 152
local player = game.Players.LocalPlayer

if player:GetRankInGroup(groupID) < rank then   
    script:Destroy()
end 


local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("PointsEvent")



while true do
    wait(15)
    remoteEvent:Fire()
end

Thanks in advance for your help.

3 Likes