Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a ban save correctly?

I'm not sure if I am doing this completely wrong but what I am trying to do is make it so when someone is teleported to a cheater cage a bool value saves and when it is true they are banned and cannot get out of a cage. But for some reason, after I looked up some tutorials they did not work.

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("BanningSystem")

game.Players.PlayerAdded:Connect(function(plr)
	local BanningSystem = Instance.new("BoolValue", plr)
	BanningSystem.Name = "IsBanned"
	
	BanningSystem.Value = ds1:GetAsync(plr.UserId) or false
 	ds1:SetAsync(plr.UserId, BanningSystem.Value)
	
	BanningSystem.Changed:Connect(function()
		ds1:SetAsync(plr.UserId, BanningSystem.Value)
	end)
end)

1 answer

Revised Server Script

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("BanningSystem")

game.Players.PlayerAdded:Connect(function(plr)
	local BanningSystem = Instance.new("BoolValue", plr)
	BanningSystem.Name = "IsBanned"
	
	local success, result = pcall(function()
		ds1:GetAsync(plr.UserId)
	end)
	
	if not success then
		warn(result)
	else
		if ds1:GetAsync(plr.UserId) then
			BanningSystem.Value = ds1:GetAsync(plr.UserId)
		else
			BanningSystem.Value = false
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds1:SetAsync(plr.UserId, plr.IsBanned.Value)
end)

A. You should wrap the datastore in a pcall so that an error doesn't break the script

B. You need to check that the datastore value is not nil, since otherwise, you are making a boolvalue's Value property nil (this would cause an error)

C. Its generally best to save the data when a player leaves using the PlayerRemoving Event, when you do this, you'd need to make sure the name of the bool matches up

D. Most of the time "ds1:GetAsync(plr.UserId) or false" will return nil if there is no data, instead of using the false value, so you need to specify false at another point in the script.

As far as the Teleport to a Cage / Keep the Player Inside, there is no attempt given so i'm not sure if you've made that yet. Make sure that when you alter the bool, you do so on the Server and not the Client.

This discussion is preserved as it was. New answers, comments, and votes are closed.