I am trying to make a datastore jail, I have the jail tool that sends the player to jail and after 30 seconds it sends them out of the jail (unjails them) but I quickly noticed people started to leave the game and rejoin. How do I fix this/make it in a datastore.
tool = script.Parent function touch(hit) h = hit.Parent g = h:findFirstChild("Humanoid") if (g ~= nil) then h:MoveTo(game.workspace.Jail.Jail.Position) wait(30) h:MoveTo(game.workspace.Jail.Unjail.Position) end end function sel() tool.Handle.Touched:connect(touch) end tool.Equipped:connect(sel)
All you have to do is put a BindableEvent "JailEvent" in ServerStorage and this should work
dss = game:GetService("DataStoreService") jailData = dss:GetDataStore("Jail") jailTimes = {} function jail(player,t) if player and t and not jailTimes[player] then --records time entered jail and sentence length jailTimes[player] = {t,tick()} local char = player.Character or player.CharacterAdded:wait() wait(1) print(player.Name .. " jailed for " .. t .. " seconds") char:MoveTo(workspace.Jail.Jail.Position) wait(t) jailTimes[player] = nil if player.Parent==game.Players then --removes jail data, only if still in jail --(jailTimes also implies whether someone is in jail) print(player.Name .. " completed his sentence") char:MoveTo(workspace.Jail.Unjail.Position) jailData:SetAsync(tostring(player.UserId),0) end end end game.Players.PlayerRemoving:connect(function(player) local times = jailTimes[player] if times then --subtracts time jailed from sentence length for remainder --will be jailed again local remaining = math.floor( times[1] - (tick() - times[2]) ) print(player.Name .. " left the game during jail. " .. remaining .. " seconds remain.") jailData:SetAsync(tostring(player.UserId),remaining) end end) game.Players.PlayerAdded:connect(function(player) --jails player if has remaining time local jailTime = jailData:GetAsync(tostring(player.UserId)) or 0 if jailTime > 0 then jail(player,jailTime) end end) --any script can jail any player with this game.ServerStorage:WaitForChild('JailEvent').Event:connect(function(player,t) jail(player,t) end)
Tool
tool = script.Parent db = false function touch(hit) if db then return end db=true local char = hit.Parent local hum = char:FindFirstChild('Humanoid') if hum then local player = game.Players:GetPlayerFromCharacter(char) if player then game.ServerStorage.JailEvent:Fire(player,30) wait(3) end end db=false end function sel() tool.Handle.Touched:connect(touch) end tool.Equipped:connect(sel)