Module Datastore script simply not working

*I am trying to get a players data by having a script detecting if a player has joined, then use a module script to get the data. It just doesn’t wanna work. There isn’t any errors.
*I’ve attempted to remove my data, going into a live server, setting it into an empty table. There isn’t any errors.
*Script:

local module = {}
local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
print("meme2")
local infods = datastore:GetDataStore("InfoDatastoreV6")
print("meme3")
local DefaultBanReason = "Cheating"
local DefaultUnbanDate = "Never."
local DefaultInfoTable = { 
	                     InAMatch = false,
	                     Wins = 0,
		              Losses = 0,
	                     Draws = 0,
	                     Banned = true,
	                     BanReason = DefaultBanReason,
	                     BanDate = "0.0.0000",
	                     UnbanDate = "Never."
		                 }

function module.GetPlayerData(player)
	if player == nil then return nil end
	local id = player.UserId
	
	local function NewUserData(useridx)
		local table1 = infods:SetAsync(useridx,DefaultInfoTable)
		return DefaultInfoTable
	end
		
	local data = infods:GetAsync(id) or NewUserData(id)
	
	if data.Banned then
		for i,v in pairs(players:GetChildren()) do
			if v.UserId == id then
				v:Kick("Banned! Reason: "..data.BanReason..". Ban date: "..data.BanDate..". Unban date: "..data.UnbanDate..".")
			end
		end
	return end
	
end

return module

I am aware that I should use pcall, and I previously did use pcall.

You never return the data from the function. Add the line

return data

to the last line in the function.

2 Likes

Oopsie, gave you the wrong version of the script. I was testing the ban system and had it set on true(still on my most recent script), and it should kick the player out of the game which it doesn’t. The tables content is all nil, nothing is what it should be.

Can you post the right version of the script? It isn’t going to be possible for someone to help you without the code you are actually using.

2 Likes

Edited it, just changed the banned from false to true. I’ve removed my data multiple times and changed the datastore name yet it doesn’t still work.

It still has the problem I mentioned earlier but it looks like the banning part should work. You don’t need to loop through all the players though as you already have a reference to the player.
e.g

	if data.Banned then
		player:Kick("Banned! Reason: "..data.BanReason..". Ban date: "..data.BanDate..". Unban date: "..data.UnbanDate..".")
		return nil
	end
	return data

data.Banned is nil, and I’ve reset my data multiple times and changed the datastore name. I have a script to handle stuff if there’s a new player joining to make data for the user. But it isn’t working.

No, that is unrelated.

Few things:

  • Don’t set data when a player joins for no reason.
  • No need to iterate through players to see if anyone is banned, just compare to Player.UserId.
  • As you’ve pointed out, call in protected mode.

That aside, it would be most wise to add print() calls to see what code is and is not running, and to also show the saving code and code that calls the module. Check to make sure that this code is all running, too.

2 Likes

Yeah that fixed it. Sorry about the inconvience. My mistake.

It only sets data if the player is new, if it isn’t in that code it is most likely for testing purposes, however thank you for your critism.