I had an error when every I join the game. There is a bool value in every player called "Banned." If it is true, the player will be kicked. But I got an error. It is supposed to save the value

This is the full code:

-->>: Datastore

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("RobloxDataStore")
local playersLeft = 0
local player_data2

-- Load leaderstats
game.Players.PlayerAdded:Connect(function(player)

	playersLeft = playersLeft + 1

	-- Leaderstats 
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	-- Coins
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = leaderstats
	
	-- Gems
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Value = 0
	gems.Parent = leaderstats
	
	-- Levels
	local levels = Instance.new("IntValue")
	levels.Name = "Level"
	levels.Value = 0
	levels.Parent = leaderstats
	
	-- Ban
	
	local banned = Instance.new("BoolValue")
	banned.Name = "Banned"
	banned.Parent = player
	
	-- Ban reason
	local banReason = Instance.new("StringValue")
	banReason.Name = "BanReason"
	banReason.Parent = banned

	-- Save data

	local PlayerUserId = player.UserId -- Player key to store data

	local player_data

	local success, errorMessage = pcall(function()
		player_data = myDataStore:GetAsync(PlayerUserId)
	end)

	-- Load data

	if success then
		coins.Value = player_data.Coins
		gems.Value = player_data.Gems
		levels.Value = player_data.Levels
		
		-- Banned players
		
		banned.Value = player_data2.Banned
		banReason.Value = player_data2.BanReason
		
		if player.Banned.Value == true then
			player:Kick("For a better experience for all players, "..banReason.Value.." is not permitted. You are banned until further notice.")
		end
	end
end)

local bindableEvent = Instance.new("BindableEvent")

-- Player leaves
game.Players.PlayerRemoving:Connect(function(player)

	local playerUserId = player.UserId -- Player key

	-- Data to be saved
	player_data2 = {
		Coins = player.leaderstats.Coins.Value;
		Gems = player.leaderstats.Gems.Value;
		Levels = player.leaderstats.Level.Value;
		Banned = player.Banned.Value;
		BanReason = player.Banned.BanReason.Value;
	}

	local success, errorMessage = pcall(function()
		myDataStore:SetAsync(playerUserId, player_data2)

		playersLeft = playersLeft - 1

		bindableEvent:Fire()
	end)

	if success then
		print("Data saved")
	else
		print("Error saving data")
		warn(errorMessage)
	end
end)

if playersLeft > 0 then
	bindableEvent.Event:Wait()
end

One part of the error is player_data2. In the banned section it says "Attempt to index nil with 'Banned.'" Banned is in the player_data2, whats the problem?

Note: I have had this problem before and I tried fixing it the same way but it didn't work

2 comments

1

Try inserting the line "local player_data2" into the "PlayerAdded" thread, between lines 66 and 67

0

It didn't work the player_data2 was underlined yellow