Why dose my Inventory system not work

My inventory system doesn’t make an intvalue change and I’m using an if statement to make it work. Basically, If the player doesn’t have an item it creates a new Intvalue the intValue has the name of the item, and the value is supposed to be the number of the item the player has

local ClickDetector = script.Parent.ClickDetector
ClickDetector.MouseClick:Connect(function()
	game.Players.PlayerAdded:Connect(function(Player)
		local Inventory = Player:FindFirstChild("Inventory")
		local IsItem = Inventory:FindFirstChild("Bread")
		if IsItem then
			IsItem.Value = IsItem.Value + 1
		else
			local newItem = Instance.new("IntValue", Inventory)
			newItem.Name = "Bread"
			newItem.Value = newItem.Value + 1
		end
	end)	
end)

I heard that your not supposed to have an event in an event but Im not
sure how to get the player without using the game.Players.PlayerAdded:Connect(function()) event

Thank you for the help : )

1 Like

Simple

script.Parent.MouseClick:Connect(function(plr)
	print(plr)
end)

this is how you get the player

1 Like

Okay so what you are doing is the following:
When someone clicks on a button, the next connection can infinite times be actived:
When a player joins then they get an item etc etc.

Instead you want to activate game.Players.PlayerAdded seperately.

game.Players.PlayerAdded:Connect(function(Player)
	local Inventory = Player:FindFirstChild("Inventory")
	local IsItem = Inventory:FindFirstChild("Bread")
	if IsItem then
		IsItem.Value = IsItem.Value + 1
	else
		local newItem = Instance.new("IntValue", Inventory)
		newItem.Name = "Bread"
		newItem.Value = newItem.Value + 1
	end
end)	

or when a button gets pressed:

local ClickDetector = script.Parent.ClickDetector

ClickDetector.MouseClick:Connect(function(Player)
	local Inventory = Player:FindFirstChild("Inventory")
	local IsItem = Inventory:FindFirstChild("Bread")
	if IsItem then
		IsItem.Value = IsItem.Value + 1
	else
		local newItem = Instance.new("IntValue", Inventory)
		newItem.Name = "Bread"
		newItem.Value = newItem.Value + 1
	end
end)

Though note, this scrip does not create an object called “Inventory” so another script should have that.