Confused with data store

Alright so I’m making a ban system and using datastores, I was going to put all the values under 1 key in the datastore (Like a large list - because this is what I’ve heard to do)

Anyways the main problem is lets say I ban the userid 1. It works and everything, but then when I ban lets say the userid 2 everything else in that list would be wiped (Like userid 1)
So basically I can only have 1 user banned at once

I’ve tried a different method of every person banned has their own key, but then I can’t grab all the keys to make like a banned list that you can view (or at least I’m not sure how)

Can you provide the code you are using?

1 Like

Yeah I’m not much of an experienced scripter, but here’s the piece that saves it

								local Data = {
								[TargetPlayer..':Banned'] = true,
								[TargetPlayer..':Reason'] = NewValue,
								[TargetPlayer..':Length'] = NewBanLength,
								[TargetPlayer..':Admin'] = Player.Name
								}
							local success, err = pcall(function()
								BanDataStore:SetAsync('List', Data)
							end)

The TargetPlayer is the players UserId
I also got the datastore editor plugin that’s how I’ve been testing it
Edit: Wait it saves to the List I forgot to change it back because I was trying something else out

First of all I would prefer to be able to see the whole script, and second of all is TargetPlayer a player instance or a userId?

Oh wait I minute I just saw you edited your post. So are you saving to the datastore under a single key?

Yes I am storing it under a single key

Why not store it under the player userId? You can check the players datastore to see if the are banned or not when they join the game.

I was going to do that I just wanted to try and make like a list you can view in game (Some other people were also saying to do that)
I guess I’ll just do that tho

You can use tables

local Data = {
    Player1 = {
        BanLength = BannedTime
    },
    Player2 = {
        BanLength = BannedTime
    }
}

But yeah, as sevasok said, you should probably use multiple keys

Heres something you could probably do for the bans

local DataStoreService = game:GetService("DataStoreService")
local PlayerModDataStore = DataStoreService:GetDataStore("PlayerModData")

game.Players.PlayerAdded:Connect(function(player)
	local loadedModData = PlayerModDataStore:GetAsync(player.UserId)
	
	if loadedModData == nil then
		loadedModData = {}
		loadedModData.Length = 0
		loadedModData.Ban = false
		loadedModData.Reason = ""
		loadedModData.Admin = ""
	else
		if loadedModData.Ban == true and (os.time() - loadedModData.BanLength) < 0 then
			player:Kick("Banned")
		elseif loadedModData.Ban == true and (os.time() - loadedModData.BanLength) >= 0 then
			loadedModData.Ban = false
			loadedModData.Length = 0
			loadedModData.Reason = ""
			loadedModData.Admin = ""
		
		end
	end
end)

1 Like