DataStore for Jail

Hello, I’ve been struggling to add a DataStore so that it saves a person’s prison time they have left to serve if they leave. I’m not sure how to SetASync if the player leaves and has jail time, and GetASync if they have jail time.

	local OriginalTeam = Player.Team
	spawn(function()
		while task.wait(1) do
			if Player:FindFirstChild("PrisonTime") then

				Player:FindFirstChild("PrisonTime").Value -= 1
				if Player:FindFirstChild("PrisonTime").Value >= 1 then
					spawn(function()
						task.wait(.5)
						if Player.Team ~= game.Teams.Jailed then
							Player.Team = game.Teams.Jailed
							task.wait(.5)
							Player:LoadCharacter()

						end
					end)
				elseif Player:FindFirstChild("PrisonTime").Value == 0 then
					Player.Team = OriginalTeam
					Player:WaitForChild("PrisonTime"):Destroy() -- Destroy Value
					Player:LoadCharacter()
				end
			end
		end
	end)
end)```

I made a rough sketch of how i would do this. I think with a few changes you can adapt it to your game

Edit: i made the code more easy to implement

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

function Save_time(plr)
	-- save the time that player has left
end

-- run when play has been sentenced to jail time
function Jail_time_countdown(plr,Time)
	for Time_Spent = Time,0,-1 do
		
		-- if player has left then we will save their time spent
		if Players:FindFirstChild(plr.Name) == nil then
			Save_time()
			break
		end
		
		task.wait(1)

		if Time_Spent <= 0 then
			-- yay we are now out of the prison!!!!!
		end
	end
end

function On_Player_Joined(plr)
	local player_Time_Left = nil -- insert the getAsync data here

	Jail_time_countdown(plr,5)
end

-- this is to make sure that all player data is saved if the server were to clsoe down
game:BindToClose(function()
	for i,plr in pairs(Players:GetChildren()) do
		Save_time()
	end
end)

Players.PlayerAdded:Connect(On_Player_Joined)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.