Hey I have looked on devforum and youtube for how to make a script like that I want but I can’t find anything related.
So I want to make when a player touch a part the game bans him for a specific time and even if he joins he will still banned until the ban ends, like this:
I did this script but the datastore does not work, I don’t see any error in the console but when I join to the game the player gets unbanned.
local DSS = game:GetService("DataStoreService"):GetDataStore("BanTest10534")
local BanPart = game.Workspace.banPart
local Time = 15000
BanPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Character = hit.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local PlayerUserId = Player.UserId
local success, errormessage = pcall(function()
DSS:GetAsync(PlayerUserId, true)
end)
if success then
print("Banned")
end
if os.time() <= os.time() + Time then
Player:Kick("\n You are banned try to join in: \n"..Time.. "m")
end
end
end)
local memoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = memoryStoreService:GetSortedMap("ban")
local banTime = 15000
workspace.BanPart.Touched:Connect(function(touchPart)
local character = touchPart.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player == nil then return end
-- add the ban time to the memorystore for 15000 seconds (the memorystore will automatically delete this key after 15000 seconds)
local success, value = pcall(sortedMap.SetAsync, sortedMap, player.UserId, os.time() + banTime, banTime)
-- kick the player
player:Kick("\n You are banned try to join in: \n" .. banTime .. "seconds")
end)
game.Players.PlayerAdded:Connect(function(player)
-- when the player enters the game check the memorystore to see if the player is on the ban list
local success, value = pcall(sortedMap.GetAsync, sortedMap, player.UserId)
-- failed roblox servers might be down we don't know if the player is banned
if success == false then return end
-- player is not banned we can simple exit
if value == nil then then return end
-- work out how long there ban has left
local deltaTime = value - os.time()
-- kick the player and tell them how long they have left
player:Kick("\n You are banned try to join in: \n" .. deltaTime .. "seconds")
end)
Hey, thanks for replying it works perfectly but the time is not decreasing it increasing I did a small change at the bottom but the time is still increasing. Is that because I replaced the datastore with the memory store service?
local BanPart = game.Workspace.banPart
local memoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = memoryStoreService:GetSortedMap("BanTest51213")
local banTime = 15000
BanPart.Touched:Connect(function(touchPart)
local character = touchPart.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player == nil then return end
local success, value = pcall(sortedMap.SetAsync, sortedMap, player.UserId, os.time(), banTime)
player:Kick("\n You are banned try to join in: \n" .. banTime .. "seconds")
end)
game.Players.PlayerAdded:Connect(function(player)
local success, value = pcall(sortedMap.GetAsync, sortedMap, player.UserId)
if success == false then return end
if value == nil then return end
local deltaTime = os.time() - value
if deltaTime >= os.time() - banTime then
player:Kick("\n You are banned try to join in: \n" .. deltaTime .. "seconds")
end
end)
ok so if you look at my code its a little different then your
im saving os.time() + banTime into the memorystore so that will be the time the ban ends
also you dont need to do this if deltaTime >= os.time() - banTime then
because the memorystore will automatically destroy itself when the ban ends and so the value will be nil when the ban ends so there is no need to do that if check
Thank you so much for your reply i mean if there is any way to make it go down and not up because I want to make it from 15000s down to 0s and then it will unban the player. Like this screenshot:
lets say os.time = 10
and lets say banTime = 10
so we are saving 20 into the memorystore local success, value = pcall(sortedMap.SetAsync, sortedMap, player.UserId, os.time() + banTime, banTime)
now lets say 5 seconds has passed so
os.time = 15
value = 20
20 - 15 = 5 that means there will be 5 seconds left until the ban ends local deltaTime = value - os.time()
deltaTime will be 5