Function running with no events?

local function playerAdded(player)

	local success, data = pcall(function ()
		return StatStore:GetAsync(player.UserId)
	end)
	if success then

		if data then
			StatService.StatCache[player.UserId] = data
		else
			StatService.StatCache[player.UserId] =
				{["Rebirths"] = 0,
					["Speed"] = 0,
					["Level"] = 0,
					["Points"] = 0,
					["Experience"] = 0
				}
		end

	else
		-- Handle case of error; kick, block saving, etc.
		warn(string.format("Could not load data for %s: %s", player.Name, data))
	end
	print(StatService.StatCache)
end

That’s it, that’s the entire script, there’s no reason why it should even run, yet it returns an error because player is nil

Which line of the code is the error coming from?

line 7
But why does the code run in the first place there is nothing calling the function?

Line 7 is a blank line

Are you properly calling your PlayerAdded event? There could be the possibility that it could be a different issue inside the script

it’s this line
warn(string.format("Could not load data for %s: %s", player.Name, data))

What is the error? That might help figure out what is causing this.

says player is nil, because the function runs on it’s own without an argument being passed

Are you calling the playerAdded function anywhere else inside the script? The thing is that if you’re accidentally calling the function without even knowing it, that could be the potential reason why

StatStore isn’t defined, is this really the full script?

nope, the function isn’t being called

Only thing I could think of is where the script is exactly located, and what kind of script is it

Is this the entire script by the way…?

no, i defined some variable previously, it’s a server script, but even weirder, I just tried running it, and it showed no error, i tried plaing the playeradded event and it returns an error when i run it despite no player being added, here is the script:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local StatStore = DataStoreService:GetDataStore("PlayerStats")
local StatService = require(game.ReplicatedStorage.Modules.StatService)




local function playerAdded(player)

	local success, data = pcall(function ()
		return StatStore:GetAsync(player.UserId)
	end)
	if success then

		if data then
			StatService.StatCache[player.UserId] = data
		else
			StatService.StatCache[player.UserId] =
				{["Rebirths"] = 0,
					["Speed"] = 0,
					["Level"] = 0,
					["Points"] = 0,
					["Experience"] = 0
				}
		end

	else
		-- Handle case of error; kick, block saving, etc.
		warn(string.format("Could not load data for %s: %s", player.Name, data))
	end
	print(StatService.StatCache)
end


local function playerRemoved(player)
	print(player)
	local success, data = pcall(function ()
		return StatStore:SetAsync(player.UserId , StatService.StatCache[player.UserId])
	end)

	if not success then
		-- Handle case of error; kick, block saving, etc.
		warn(string.format("Could not load data for %s: %s", player.Name, data))
	end
end

Players.PlayerRemoving:Connect(playerRemoved())

Players.PlayerAdded:Connect(playerAdded())

How did that not error as a nil value

You’re calling the function twice, if you change your Events to these:

Players.PlayerRemoving:Connect(playerRemoved)
Players.PlayerAdded:Connect(playerAdded)

You should be all good to go!

2 Likes