Variable showing as nil

local function giftHandler()
	local function giftPurchaseFinished(player, purchaseID, status)
		if status == true then
			print(player)
			local playerName = game.Players:GetPlayerByUserId(player)
			print(playerName.Name)
			local playerToGiftId = playerName:FindFirstChild("OtherStats"):FindFirstChild("GiftTo").Value
			print(playerToGiftId)
			local playerToGift
			for i, playerInGame in ipairs(game.Players:GetPlayers()) do
				if playerInGame.UserId == playerToGiftId then
					playerToGift = playerInGame.Name
					print(playerToGift.." Got gifted gamepass")
					break
				else
					print(playerInGame.UserId)
					print(playerToGiftId)
					print("No")
				end
				
			end
			if purchaseID == 1241886579 then
				print(playerToGift.Name)
				if playerToGift.OtherStats then
					print("Other Stats")
					if playerToGift.OtherStats.Gamepasses then
						print("Gamepasses")
					end
				end
				local val = Instance.new("StringValue",playerToGift:FindFirstChild("OtherStats"):FindFirstChild("Gamepasses"))
				val.Name = "X2Seeds"
			end
			playerName:FindFirstChild("OtherStats"):FindFirstChild("GiftsGiven").Value += 1
			print("Finished")
		end
	end
	MPS.PromptProductPurchaseFinished:Connect(giftPurchaseFinished)
	remotes.GiftRemote.OnServerEvent:Connect(function(playerGifting, playerToGift)
		if playerGifting:FindFirstChild("OtherStats"):FindFirstChild("GiftTo") then
			local GiftToID = game.Players:GetUserIdFromNameAsync(playerToGift)
			playerGifting.OtherStats.GiftTo.Value = GiftToID
		end
	end)
end


this line is printing nil

print(playerToGift.Name)

This line is erroring because it is nil

cal val = Instance.new("StringValue",playerToGift:FindFirstChild("OtherStats"):FindFirstChild("Gamepasses"))

I dont know why it is nil if it is printing(line 215 in the picture, and this line of code)

print(playerToGift.." Got gifted gamepass")

Is it because it is being changed in the loop?

It’s because cal isn’t an actual function/ thing you can call. Use this instead for that line:

local val = Instance.new("StringValue",playerToGift:FindFirstChild("OtherStats"):FindFirstChild("Gamepasses"))

He is using ‘local’, its just cut off in the one line (If you look at the actually code he uses local)

Isnt that the exact same as what is currently there?

Oh, sorry. I didn’t see that.

30

On this line: print(playerToGift.Name) you don’t need to say .Name, instead just say: print(playerToGift). So because playerToGift is a string value and not an instance, its giving you the error on line 232.
So I believe you should just be able to say :
local val = Instance.new("StringValue",game.Players.playerToGift:FindFirstChild("OtherStats"):FindFirstChild("Gamepasses"))

Thank you, I thought I declared it as a player not their name

No problem, yea on this line: playerToGift = playerInGame.Name your declaring it as their name