Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my script removing all tools instead of only certain tools?

I want this to prevent from spawning tools in my game so I made a list of all of the tools that are in my game and tried making a script so that if the player has a tool that does not match the list that I have then it will get removed, issue here is that it is removing all of the tools, even the ones in the game.

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
	while true do wait()
		wait(0.5)
		for i, v in pairs(player.Backpack:GetChildren()) do
			if v.Name ~= "2DGame" or v.Name ~= "SelfieStick" or v.Name ~= "Suitcase" or v.Name ~= "Coffee" or v.Name ~= "FirstAid" or v.Name ~= "HoverBoard" or v.Name ~= "SegwayHoverboard" or v.Name ~= "SoccerBall" or v.Name ~= "SuperFlyGoldBoombox" or v.Name ~= "Sword" then
				v:Destroy()
			end
		end
	end
	end)
end)

Note that I had more but I removed it to make it simpler for you guys. How can I fix this?

1 answer

You were using 'or' instead of 'and'. Anyway the way you're doing it seems impractical so do something similar to this. v v v

tools = {"2DGame","SelfieStick","Suitcase","Coffee","FirstAid","HoverBoard","SegwayHoverboard","SoccerBall","SuperFlyGoldBoombox","Sword"}

game.Players.PlayerAdded:connect(function(player)	--Player joins
    player.CharacterAdded:connect(function(character)	--Character Added
   	 while true do wait()
        		wait(0.5)
        	for i, v in pairs(player.Backpack:GetChildren()) do	--Cycles through backpack
		  local exist = false	--Boolean to check if the tool's name is in the table
           		for i, t in pairs(tools) do	--Cycles through table
				if v.Name == t then	--Checks to see if the tool's name matches any of the contents of the table 
					exist = true  --If so then it sets exist to true and breaks out of the loop
					break
				end
	   		end
			if exist == false then  --Ran after the loops are done and since the tool doesn't match one in the table, it gets destroyed
				v:Destroy()
			end
        	end
    	end
    end)
end)

Just did this off the top of my head so there is undoubtedly some errors. Hope this works for you.

4 comments

0

That is a very good script and it works, I am thankful for your help. I am trying to wrap my head around how it works however and that is puzzling me.

0

Please tell me if my interpretation is correct. As character loads, a while true do loads and then it starts iterating whatever is in player.Backpack and then it iterates whatever is in the tools table. If something in the Backpack has the same name as something in the tools table, it will set exist=true and then break out of the "for i, t in pairs(tools) do" and then move back to the first loop w

0

I got cut off but its fine. What happens...oh I got it now. If something does not match, it never runs through that "if v.name == t then", or it does but it simply just goes to the two ends and then that "if exist ==false" will run because exist was set to false previously.

0

Apologize for taking so long. And you're pretty spot on. The 'for' loop and arrays/tables are a pretty powerful thing when it comes to coding in ,well,anything. Roblox has a lot of useful functions and i'm pretty sure if you go to the wikia they'll provide a snippet of code to help you out,

This discussion is preserved as it was. New answers, comments, and votes are closed.