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

Script that destroys weapons in a backpack if in a certain group?

local groupId = 3438449 local tool = game.StarterPack.Musket

function onPlayerSpawned(player) if player:IsIngroup(groupId) then tool:Remove().Parent = player.Backpack.Musket end end

game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function() onPlayerSpawned(player) end) end)

This is what I got, its meant to remove the Musket tool if in the groupID at the top, It isn't working and I can't seem to get it to work.

1 comment

0

If you want your code to be more readable, you can edit it and put the code into the block created by pressing the blue lua button.

1 answer

So your code got really close to being correct. The problem here is that your variable tool is a reference to a single instance and does NOT change from player to player.

The tool in StarterPack is actually cloned by the game and put in as a seperate instance in the player's backpack. That means if we have a variable storing a reference to the StarterPack tool, then when a player spawns our variable is still out-of-date.

The solution is to create a variable pointing to the new tool.

local groupId = 3438449

function onPlayerSpawned(player)
   if player:IsIngroup(groupId) then
      local tool = player.Backpack:FindFirstChild("Musket")
      if tool then tool:Remove() end -- We need to check that the Musket is not nil.
   end
end

game.Players.PlayerAdded:connect(function(player)
   player.CharacterAdded:connect(function() 
      onPlayerSpawned(player)
   end)
end)

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