Values in module script are nil

Hi you all. I’m developing an obby game with a round system, and there is a placement system (1st place, 2nd place, etc.)

For some reason, one specific part of the placement system does not work, because the list is nil. When I actually print it in the module script, it prints the list, so I don’t know why it is nil in another server script referencing it. I tried finding out if something was in the wrong order, but I couldn’t find out what to do.

Here is the part where it says it is nil:

place = competiviveModule.competitivePlacements[plr.UserId] --Where it returns nil

Well here is the actual code:

When Round Ends (Part of Server Script)
--Function that handles the competitive placement system rewards
local function competitivePlacementDisplay(plr)
	
	--Variables
	local place
	local reward
	local err, success = pcall(function()
		place = competiviveModule.competitivePlacements[plr.UserId]
		reward = math.ceil(300/place)
	
		print(reward)
		print(place)
	end)
	
	if not success then
		warn(plr.Name .. " placement data not loaded")
	end
	
	
	--Placements will be shown to all players with a GUI
	competiviveModule.competitivePlacementsGuiDisplay(plr, isFirstIntermission)
	
	--Checks if the player should go onto the winners podium
	if place and (place <= 3) then
		
		local top3List = {
			[1] = firstPart,
			[2] = secondPart,
			[3] = thirdPart
		}
		
		--Variables
		local podiumPart = top3List[place]
		local positionPart = podiumPart:WaitForChild("PositionPart")
		local nameLabel = podiumPart:WaitForChild("SurfaceGui"):WaitForChild("NameLabel")
		
		--Get the player's character
		local character = plr.Character or plr.CharacterAdded:Wait()
		local newCharacter
		
		if character then
			character.Archivable = true
			newCharacter = character:Clone()
			character.Archivable = false
			newCharacter.Parent = podiumPart
		end
		
		local humanoid
		local humanoidRootPart
		if newCharacter then
			
			humanoidRootPart = newCharacter:FindFirstChild("HumanoidRootPart")
			humanoid = newCharacter:FindFirstChild("Humanoid")
			
			--Apply Character to podium
			humanoidRootPart.CFrame = positionPart.CFrame
			humanoidRootPart.Anchored = true
			nameLabel.Text = plr.Name
			nameLabel.Visible = true
			
			--Animate Character
			local danceAnimation = danceAnimList[math.random(1,3)]
			
			local animator = humanoid:WaitForChild("Animator")
			local danceTrack = animator:LoadAnimation(danceAnimation)
			danceTrack:Play()
			
		end
				
	end
	
end

When function is called:

--Reward Players and Respawn Players at Lobby
	for i, player in pairs(Players:GetPlayers()) do
		
		--(Other code was here not related to this question)
		
		--Podium Display
		competitivePlacementDisplay(player)

	end
Module
local module = {
	
	competitivePlacements = {},
	
	
	displayProperties = {
		
		[1] = {"1st", Color3.fromRGB(255, 184, 70)},
		[2] = {"2nd", Color3.fromRGB(239, 239, 239)},
		[3] = {"3rd", Color3.fromRGB(197, 121, 70)},
		[4] = {"4th", Color3.fromRGB(113, 113, 113)},
		[5] = {"5th", Color3.fromRGB(113, 113, 113)},
		[6] = {"6th", Color3.fromRGB(113, 113, 113)}
		
	}
	
}

function module.competitivePlacementsManagement(plr)
	
	module.competitivePlacements[plr.UserId] = #(module.competitivePlacements) + 1
	
	print(module.competitivePlacements)
	
	local place = module.competitivePlacements[plr.UserId]
	local reward = math.ceil(300/place)
	
	return reward
	
end


function module.competitivePlacementsGuiDisplay(plr, isFirstIntermission)
	
	--Variables
	local WinGuiDisplayEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents"):WaitForChild("WinDisplayEvents"):WaitForChild("WinGuiDisplay")
	local WinGuiVisibleEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents"):WaitForChild("WinDisplayEvents"):WaitForChild("WinGuiVisible")
	
	local PlacementFrame = plr:WaitForChild("PlayerGui"):WaitForChild("WinnersGui"):WaitForChild("PlacementFrame")
	
	
	if not isFirstIntermission then
		
		if module.competitivePlacements[plr.UserId] then
			
			local place = module.competitivePlacements[plr.UserId]
			local info = module.displayProperties[place]
			
			print(place)
			print(info)
			
			WinGuiDisplayEvent:FireClient(plr, info[1], info[2])
			
			print("placements list")
			
		end
		
		WinGuiVisibleEvent:FireClient(plr, PlacementFrame)
				
	else	
		print("First Intermission, no placements list")
	end
end


return module

When player enters the portal that makes you win (Part of a different module script which gets ran in a Server Script)
--Rewarding Players for Winning
local reward = competiviveModule.competitivePlacementsManagement(player)

--Just know that there is a lot of unrelated code that I'm just not showing, so just know that this variable is declared when the player reaches the end portal.

Thank you all for helping :happy2:

1 Like

im really bad at scripting but i think you need require(module script) to get the values but other than that idk sry

are you putting that value from a localscript and trying to see it from the server? or the other way around?? (module scripts will not assign that value through each machine)

I can’t find the competitivePlacements() function in your module.

so that would be

local module = require(PUT MODULE SCRIPT PATH) --this should be a variable at the start of your script

place = module.competitivePlacements[plr.UserId]

im really sorry if it doesnt work

It is all server sided, so it should be working

I already required the module in the script, i just showed a function in the script.

kinda feel this isnt helpful. a question from me what exactly returns nil in that?
because if its plr then its when u are calling the function.
but if its competiviveModule.competitivePlacements then can i see where that is or something like that?

Nevermind, I thought I named the function wrongly but I didn’t. For some reason I thought the table was a function returning something for a second lol

It’s all in the post, just open the arrows

ikr BUT where is the place that the functions calls???

Near the beggining of the first script I show, around the 5th line.

but thats the function itself thats not like func() ← this is a call

It gets the table from the module, and then it indexes that table with the player’s UserId, I have no idea why this doesnt work

competiviveModule.competitivePlacements is a table

did u do this in anywhere in the script

competiviveModule.competitivePlacements[plr.UserId] = "idk the data u maybe want?"

In the module script:

function module.competitivePlacementsManagement(plr)
	
	module.competitivePlacements[plr.UserId] = #(module.competitivePlacements) + 1
	
	print(module.competitivePlacements)
	
	local place = module.competitivePlacements[plr.UserId]
	local reward = math.ceil(300/place)
	
	return reward
	
end

i dont see anything wrong but 2 things i wanna ask can i see the error ur getting and its maybe u are calling this function just after u called the other function which makes this nil

Alright here is the error if I don’t use the pcall:

ServerScriptService.Main:252: attempt to perform arithmetic (div) on number and nil
Stack Begin
Script ‘ServerScriptService.Main’, Line 252 - function competitivePlacementDisplay
Script ‘ServerScriptService.Main’, Line 516 - function intermissionStart
Script ‘ServerScriptService.Main’, Line 555 - function mainGame
Stack End

from my pov it seems u are dividing a nil number with an existing number and can u show where that is happening?

We are trouble shooting … we need to see everything.