For loops only working sometimes?

I’m making a sell-crate system where if you click a button, A localScript fires a remote event to the server to use a for loop to gather all the wheat in the players Inventory and then gives him money for every loop.

local Rs = game.ReplicatedStorage
local WheatSold = Rs.Events["Wheat Based"].WheatSold

WheatSold.OnServerEvent:Connect(function(player)
	local succ, err = pcall(function()
		if player.Backpack:FindFirstChild('Wheat') then
			for i, v in pairs(player.Inventory:GetChildren()) do
				if v.Name == 'Wheat' then
					player:WaitForChild('leaderstats').Cash.Value += 2
					print('Sold!')
					v:Destroy()
				end
			end
			for i, v in pairs(player.Backpack:GetChildren()) do
				if v.Name == 'Wheat' then
					v:Destroy()
				end
			end
			for i, v in pairs(player.Character:GetChildren()) do
				if v.Name == 'Wheat' then
					v:Destroy()
					
				end
			end	
		end	
	end)
	if succ then
		print('Sold Succesfully')
	else
		warn(err)
	end
end)

This is the server sided script.


Button.MouseButton1Up:Connect(function()
	if game.Players.LocalPlayer.Inventory:FindFirstChild('Wheat') and game.Players.LocalPlayer.Backpack:FindFirstChild('Wheat') or game.Players.LocalPlayer.Character:FindFirstChild('Wheat') then
		local player = game.Players.LocalPlayer
		local Sold = game.ReplicatedStorage.Events["Wheat Based"].WheatSold
		Sold:FireServer(player)
		print('Fired!')
	else
		print('No Wheat')
	end
end)

The scripts only work sometimes like 1 in every 15 tries, my ping is 104 ms which is good enough for testing, Whats the problem with it?

Try this, i cut some unnecessary code

local Rs = game.ReplicatedStorage
local WheatSold = Rs.Events["Wheat Based"].WheatSold

WheatSold.OnServerEvent:Connect(function(player)
	local succ, err = pcall(function()
		if player.Backpack:FindFirstChild('Wheat') then
			for i, v in pairs(player.Inventory:GetChildren()) do
				if v.Name == 'Wheat' and player:FindFirstChild("leaderstats") then
					player.leaderstats.Cash.Value += 2
					print('Sold!')
					v:Destroy()
				end
			end
			
		end	
	end)
	if succ then
		print('Sold Succesfully')
	else
		warn(err)
	end
end)
1 Like

The excess code was necessary because The inventory was a saving system file, I tried doing it without the other code anyway but it only works once after you join, After that it says it “fires” but doesn’t really.
Same with the excess code too.

The code only works once when you join, But doesn’t work the second time…

Can you pin-point exactly where it isn’t working? You say the local script only works 1 in 15 tries? Does it print ‘Sold Succesfully’ 1 in 15 times?

This expression is a little concerning, I would recommend putting parenthesis around long logical expressions. Though I am not sure this is the error you want fixed today.

game.Players.LocalPlayer.Inventory:FindFirstChild('Wheat') and (game.Players.LocalPlayer.Backpack:FindFirstChild('Wheat') or game.Players.LocalPlayer.Character:FindFirstChild('Wheat'))
1 Like

This was the line I would suggest looking at as well. It will only execute if there is wheat in both the Inventory AND the backpack OR in the character.

It used to work i think every 15 tries but now it really just works once whenever i join. it still prints sold successfully but never actually increase my cash. I don’t want it fixed today, I just want it fixed. The output says the prints saying that it works but it never actually gives me the money afterwards. I tried making it print v. It works after i join but the second time i try it in that session v never gets past the if function, The parrenthesis doesn’t affect it much too.

Does it delete the wheat? Note that your for loops only give cash if the wheat is in player.Inventory without more context maybe the Inventory is empty?

1 Like

Yes, It deletes the wheat. Then you gather more wheat then sell it again and repeat the process. I do have server sided wheat in my Inventory folder.

Edit : it turns out i was giving the wheat to the player backpack on the client side and not the server side

1 Like

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