My if statement isn't working correctly

Note: I’m kinda new to roblox scripting

What I’m trying to do is check if a meme has been collected already and if it hasn’t, the if statement will go on. But for some reason the if statement always goes on even if the meme is already inside the table. :sob:

Code:

local remote = game:GetService("ReplicatedStorage"):WaitForChild("Remote")

local memesCollected = {}

for _, meme in pairs(workspace.Memes:GetChildren()) do
	meme.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			if not memesCollected[player] then -- if player doesn't have a table
				memesCollected[player] = {}
			end
			if not memesCollected[player][meme.Name] then -- if meme isn't already collected (doesn't work for some reason)
				local char = hit.Parent
				local plr = game.Players:GetPlayerFromCharacter(char)
				table.insert(memesCollected[player], meme.Name)
				remote:FireClient(plr, meme, true)
			end
		end
	end)
end

Ask me anything!

Change the third conditional statement to:

if not table.find(memesCollected[player], meme.Name) then

The problem is that you’re trying to find a specific index inside of the memesCollected[player] array, which never exists since all you do is add an element to the memesCollected[player] array.

Ahh I see, thanks so much! Some reason I thought you didn’t need to do table.find.

No problem! You can also remove the plr variable since you already have the player variable declared above.

1 Like

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