I want to be able to save all arrests with the current time (e.g if they leave with 60 seconds remaining, when the rejoin they still get 60 seconds jail time).
It’s just that I’ve looked basically everywhere for something on datastorage or how to-do this and I’ve found nothing so far.
How I currently calculate a persons jail time is using String Values and just getting the data that way for how long the player is in jail.
Section of code: (if it may assist)
Teams.Arrested.PlayerAdded:Connect(function(player)
local arrestedui = player:WaitForChild("PlayerGui"):WaitForChild("Arrested")
local c = ArrestTimeVal:Clone()
c.Value = ArrestTimeVal.Value
c.Parent = arrestedui
arrestedui.Enabled = true
wait(ArrestTimeVal.Value + 1)
player.TeamColor = BrickColor.new("Medium stone grey")
c:Destroy()
arrestedui.Enabled = false
player:LoadCharacter()
end)
Teams.Arrested.PlayerAdded:Connect(function(player)
local arrestedui = player:WaitForChild("PlayerGui"):WaitForChild("Arrested")
local timetext = arrestedui.ArrestedUI.time
local arresttime = arrestedui:WaitForChild("ArrestTime")
for i = arresttime.Value, 0, -1 do
timetext.Text = "Time Remaining: "..i..""
wait(1)
end
end)
It’s not the arrest time that’s relevant, but time remaining to-be-served. This data should be linked with the player’s session data as it is something you want saved. When an arrest happens, the time sentence is set and then decremented as they serve time. When the player disconnects from the server, save the time served alongside whatever other session data you have. When you download this data, load the time served back into the remaining sentence, then continue decrementing until 0
Basically create a number value inside of the player when they join, then if they go to jail set the number value to the time the must serve and incrementally decrease the value. If they leave all you need to do is check if the value is not zero and add it to a table which you then save to a datastore (make sure to use pcalls incase it doesn’t save or load). Once they join back load the datastore and and set the the value that is created in the player to the one in the datastore; you can also run a function to send them to jail if they have time to serve. If you want to know more about datastores I suggest you go on the roblox documents for datastores.
local DataStoreService = game:GetService("DataStoreService")
local JailData = DataStoreService:GetDataStore("JailTimes")
local function saveJailTime(player, remainingTime)
JailData:SetAsync(player.UserId, os.time() + remainingTime)
end
local function loadJailTime(player)
local storedTime = JailData:GetAsync(player.UserId)
if storedTime then
local remainingTime = storedTime - os.time()
return math.max(remainingTime, 0)
end
return nil
end
Teams.Arrested.PlayerAdded:Connect(function(player)
local arrestedui = player:WaitForChild("PlayerGui"):WaitForChild("Arrested")
local arresttime = arrestedui:WaitForChild("ArrestTime")
local timetext = arrestedui.ArrestedUI.time
local remainingTime = loadJailTime(player) or ArrestTimeVal.Value
arresttime.Value = remainingTime
player.AncestryChanged:Connect(function()
if not player.Parent then
saveJailTime(player, arresttime.Value)
end
end)
arrestedui.Enabled = true
for i = remainingTime, 0, -1 do
timetext.Text = "Time Remaining: " .. i
arresttime.Value = i
wait(1)
end
player.TeamColor = BrickColor.new("Medium stone grey")
arrestedui.Enabled = false
player:LoadCharacter()
JailData:RemoveAsync(player.UserId)
end)
It doesn’t exactly work. I can jail the player and rejoin the player. However when I rejoin it won’t send you to prison and instead will require you to jail the player once again. But thanks for your time writing the script.
Update that could help: Whenever I jail a player that’s already been jailed before, it will set the timer to the time but then it will load the data which is normally at 0 for some reason.
You can set a variable for already jailed players and the cell they were in, and prevent the script from assigning players to those if its occupied, and on player join check if pre assigned first. you can do this by using tables.
That wasn’t exactly the issue. After looking closer in the script you sent it expected the value to already be there. However for me and this is what I normally do for things like this is clone the value once the player is arrested. I could rewrite the script but that will take quite some time as this is what I’m used to. Sorry!
I’ll send the timer and sentence script bellow and if you // someone thinks it’d be best for me to rewrite it for this to be easier that’d be good.
-- // TEMPERARY ARREST TIME SYSTEM
local rps = game:GetService("ReplicatedStorage")
local ArrestTimeVal = rps.ArrestSystem.ArrestTime
local Players = game:GetService("Players")
Teams.Arrested.PlayerAdded:Connect(function(player)
local arrestedui = player:WaitForChild("PlayerGui"):WaitForChild("Arrested")
local c = ArrestTimeVal:Clone() -- Section that may cause datastore to mess up
c.Value = ArrestTimeVal.Value
c.Parent = arrestedui
arrestedui.Enabled = true
wait(ArrestTimeVal.Value + 1)
player.TeamColor = BrickColor.new("Medium stone grey")
c:Destroy()
arrestedui.Enabled = false
player:LoadCharacter()
end)
Teams.Arrested.PlayerAdded:Connect(function(player)
local arrestedui = player:WaitForChild("PlayerGui"):WaitForChild("Arrested")
local timetext = arrestedui.ArrestedUI.time
local arresttime = arrestedui:WaitForChild("ArrestTime")
for i = arresttime.Value, 0, -1 do
timetext.Text = "Time Remaining: "..i..""
wait(1)
end
end)
If you’d like to use discord to communicate easier (if you have it) here’s my username:
nonotess