Cant figure out why this value is nil?

server script
on the server script, rewardValue, it prints the value (100). However when I run the OnClientEvent in the local script, it prints nil. I have no clue why

local npcRewards = {
	{name = "Dummy", reward = 100},
	{name = "Boss", reward = 1000},
}
for _, npc in pairs(NPCFolder:GetChildren()) do


	npc.Humanoid.Died:Connect(function()
			local name = npc.Name
			local humanoid = npc:FindFirstChild("Humanoid")
			local tag = humanoid.creator
		
		if tag then
			print("hello")
			if tag.Value ~= nil then
				
				local playerId = Players:GetUserIdFromNameAsync(tostring(tag.Value))
				local player = Players:GetPlayerByUserId(playerId)
				for i, v in pairs(npcRewards) do
					if v.name == name then
						
						local rewardValue = v.reward
						print(rewardValue)
						onNPCKilled(player,tonumber(rewardValue))
						
						break
					end
				end
				
			end
		end
		
	end)
end

local script

-- LocalScript in StarterGui

local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local CurrencyGui = PlayerGui:WaitForChild("CurrencyGui")
local imageButton = CurrencyGui:WaitForChild("CashButton")
-- Settings
local moneyImageId = "rbxassetid://13725048276"
local animationTime = 1.5
local coins = game.Players.LocalPlayer:FindFirstChild("leaderstats").Coins.Value
-- Cash Text Object
local cashText = CurrencyGui:WaitForChild("CashButton"):WaitForChild("CashText") 
local cashIcon = imageButton:WaitForChild("cashimage")
local currentCash = 0
local popCashEvent = game.ReplicatedStorage:WaitForChild("PopCash")


local function createMoneyPopup(rewardValue)
	print(rewardValue)
	
	local imageLabel = Instance.new("ImageLabel")
	imageLabel.Image = moneyImageId
	imageLabel.Size = UDim2.new(0, 100, 0, 100)
	imageLabel.Position = UDim2.new(0.5, -50, 0.5, -50)
	imageLabel.AnchorPoint = Vector2.new(0.5, 0.5)
	imageLabel.BackgroundTransparency = 1
	imageLabel.ImageTransparency = 1 -- Start transparent
	imageLabel.Parent = CurrencyGui

	-- Fancy animation: Fade in, move up, then fade out
	imageLabel:TweenSize(UDim2.new(0, 150, 0, 150), "Out", "Bounce", animationTime, false)
	imageLabel:TweenPosition(UDim2.new(0.5, -50, 0.3, -50), "Out", "Quad", animationTime, false)

	
	for i = 1, 0, -0.05 do
		imageLabel.ImageTransparency = i
		wait(0.05)
	end

	-- Wait before starting fade out
	wait(animationTime * 0.7)
	
	local cashIconPosition = cashIcon.AbsolutePosition -- Get the position of the cash icon
	local cashIconSize = cashIcon.AbsoluteSize
	local targetPosition = UDim2.new(0, cashIconPosition.X + cashIconSize.X / 2, 0, cashIconPosition.Y + cashIconSize.Y / 2)

	imageLabel:TweenPosition(targetPosition, "Out", "Quad", animationTime, false)

	for i = 0, 1, 0.05 do
		imageLabel.ImageTransparency = i
		wait(0.05)
	end

	imageLabel:Destroy()

	
	local startCash = currentCash
	
	currentCash = currentCash + rewardValue
	for i = startCash, currentCash, math.ceil((currentCash - startCash) / 30) do
		cashText.Text = "$" .. i
		wait(0.03)
	end
	cashText.Text = "$" .. currentCash
end



popCashEvent.OnClientEvent:Connect(function(player,rewardValue)
	print(rewardValue) --NIL
	createMoneyPopup(rewardValue)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

May I see how you’re firing the client event from the server side?

1 Like

try printing player and if it returns 100 then instead of doing

OnClientEvent:Connect(function(player,rewardValue)

do

OnClientEvent:Connect(function(rewardValue, player)

(basically reverse them)

1 Like
popCashEvent.OnClientEvent:Connect(function(player,rewardValue)
	print(rewardValue) --NIL
	createMoneyPopup(rewardValue)
end)

its probably because the player value isnt set when firing, so your “rewardValue” is actually stored in “player” rather then intended, just remove the player value and it should work

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.