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

Why is this script only destroying 1 of 2 pistols?

So what I have done is I have created a pistol with wiki help, and I cloned it so there are 2 pistols in a game space. What is supposed to happen is when you pick up both, both are destroyed and you are given dual pistols. The problem is, only one of the pistols are destroyed and the player still gets the dual pistols. So, what am I doing wrong? (both pistols are exactly the same)

Here is the script:

local tool = script.Parent
local user
local ammo = 12
local fireIco = 'rbxasset://textures/GunCursor.png'
local reloadingIco = 'rbxasset://textures/GunWaitCursor.png'
function checkForOtherPistols()
	local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	if script.TimesRun.Value == 0 then
		player.Pistols.Value = player.Pistols.Value + 1
		script.TimesRun.Value = 1
		if player.Pistols.Value == 2 then
			local clone = game.Lighting["Dual Pistols"]:Clone()
			clone.Parent = player.Backpack
			wait(1)
			print("Destroying a pistol...")--Debug Helper
			script.Parent:Destroy()
		end
	end
end
--when the tool is equipped
tool.Equipped:connect(function(mouse)
	checkForOtherPistols()
	mouse.Icon = fireIco
   --store the character of the person using the tool
   user = tool.Parent 

   --when the left mouse button is clicked
   mouse.Button1Down:connect(function() 
      if ammo ~= 0 then --make and do a hit test along the ray
       local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit*300)
       local hit, position = game.Workspace:FindPartOnRay(ray, user)

       --do damage to any humanoids hit
       local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
       if humanoid then
			if hit.Name == "Head" then
				humanoid:TakeDamage(10000000)
			end
           humanoid:TakeDamage(30)
       end

       --draw the ray
       local distance = (position - tool.Handle.CFrame.p).magnitude
       local rayPart = Instance.new("Part", user)
       rayPart.Name          = "RayPart"
       rayPart.BrickColor    = BrickColor.Yellow()
       rayPart.Transparency  = 0.5
       rayPart.Anchored      = true
       rayPart.CanCollide    = false
       rayPart.TopSurface    = Enum.SurfaceType.Smooth
       rayPart.BottomSurface = Enum.SurfaceType.Smooth
       rayPart.formFactor    = Enum.FormFactor.Custom
       rayPart.Size          = Vector3.new(0.2, 0.2, distance)
       rayPart.CFrame        = CFrame.new(position, tool.Handle.CFrame.p) * CFrame.new(0, 0, -distance/2)

       --add it to debris so it disappears after 0.1 seconds
       game.Debris:AddItem(rayPart, 0.1)
		ammo = ammo - 1
		script.Fire:Play()
	else
		for i = 1, 12 do
			script.Reload:Play()
			mouse.Icon = 'rbxasset://textures/GunWaitCursor.png'
			wait(0.5)
		end
		ammo = 12
		mouse.Icon = fireIco
	end
   end)
end)

1 answer

Alright, I recommend changing the name of the dual pistols to what ever the name is but with dual in its name.

then try doing the following for your Destroying part of the script.

--Only works if your gun script is a localscript.
local Player = game.Players.LocalPlayer

for i,v in pairs(Player.Backpack:GetChildren()) do
if v.Name == script.Parent.Name then
v:Destroy()
end
end

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