[tough cookie] Table.remove is not working properly. has something to do with looping through character

i have this function that is supposed to remove the player from the table if “cube” doesn’t exist. instead if the cube exist it constantly adds and removes player from the table. but when it removes it removes 26x exactly each time. i’ve wrote this several different ways. and still same thing. except one way i wrote it. it did remove only one time each time it removed. still had the continuous loop though. very strange. i’ve never had this issue. i’m assuming it has something to do with the character. doesn’t make any sense.

this is what the script is now.

function module.remove(player, person)
	if not table.find(module.has, player) then return end
	
	local me = table.find(module.has, player)
	
	for i, v in ipairs(person:GetChildren()) do
		if v:GetAttribute("cube") then return end
		
		table.remove(module.has, me)
		print"removed me"
	end
end
2 Likes

I’ve had this same issue that I had to deal with this week, the remove method was not actually removing it from the table. Try setting the index to nil instead of using table.remove

function module.remove(player, person)
	if not table.find(module.has, player) then return end
	
	local me = table.find(module.has, player)
	
	for i, v in ipairs(person:GetChildren()) do
		if v:GetAttribute("cube") then return end
		
		module.has[me] = nil
		print"removed me"
	end
end
3 Likes

still getting the same issue. this is so broken. bbruh

2 Likes

i just don’t get it. works perfectly for regions but for looping through character it is completely broken.

2 Likes

Must be a problem with your module then. I still reccommend replacing the usage of table.remove with setting the index to nil.

Could you post the snippet of the block where that function is called?

2 Likes
game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local human = character:WaitForChild("Humanoid")
	if not human then return end
	
	while character.Parent == nil do
		character.AncestryChanged:wait()
	end
	
	local newAnim = human:LoadAnimation(anim)
	while wait() do
		module.add(player, character)
		module.remove(player, character)
		
		local result = module.check(player)
		if not result then continue end
		if playing then continue end
		
		if result then
			newAnim:Play()
			playing = true
			print"im supposed to be playing.."
		end
	end
end)
2 Likes

i’m really just experimenting with tables and it seems i’ve chosen the most difficult task haha. crazy

1 Like

Why is module.remove being called right after module.add?

1 Like

that’s how i did it for regions. works perfect for that.

2 Likes

In this block, you’re running the print command for every child of the character. Maybe it has to do with that? Pretty sure that if you put a “print(#person:GetChildren())” before the loop you will see it print 26 as well.

1 Like

that is it and the thing is i got it to only print “removed” once but it still was continuous.

2 Likes

The keyword “break” can be used to stop a loop. Try putting it inside of your for loop at the end and see if that works

2 Likes

already have. still continuous just prints the one time.

2 Likes

like instead of 26x
dghjdgyjdgjgdhj

1 Like

What do you mean by continuous? That it keeps getting called? If so it might be the while loop that it’s being called in.

2 Likes

basically no matter what. while the cube is in possession it removes. but when the cube gets destroyed thats when it stops.

1 Like

thats what i cant figure out why

2 Likes

no it’s not supposed to be called when i have the cube. just called when i don’t have the cube. the while loop, i mean, is not the issue. least i doubt. because if it was then i’d have issues like this with the other scripts. they are just inserting player to table if has cube. and removing player from table if doesn’t have cube. but as long as player has cube. its constantly getting added and removed when he has it. hes only supposed to be removed if he doesnt have it

1 Like

I guess module.check returns whether or not the player is holding the cube right?

2 Likes

yeah but that’s just for the animation. not for removing.

1 Like