Hi, To Anyone Who is reading this, I've been trying to script a buy gun script and when you buy it and then you run out of ammo you can buy ammo from the button and you dont have to buy another gun and it worked only when i got the gun and not the ammo when i clicked on it twice, for some reason i had trouble trying to find out how to fix it for many hours and i came upon that i have no idea so heres the script

It would be kind if you fixed it :)

local Button = script.Parent
GunCost = 500 
AmmoCost = 250
dist = 5 
local lighting_gun = game.Lighting:FindFirstChild("M14")

function onClicked()
	local p = game.Players:GetChildren()
	for i = 1, #p do 
        local points = p[i].leaderstats:findFirstChild("Points")
		if (p[i].Name ~= script.Owner.Value) then 
                if  points.Value > GunCost or points.Value == GunCost then 
				if (p[i].Character.Torso.Position - script.Parent.Position).magnitude < dist then
					GUN = game.Lighting:findFirstChild("M14"):clone()
					if p[i].Backpack:findFirstChild(GUN.Name) ~= nil then return end	
					Button.Text.Value = "Hold F Weapon ["..GunCost.."], Ammo ["..AmmoCost.."], Upgraded Ammo [4500]"
			--{{-- else
				--	local GUN = player.BackPack:FindFirstChild("M14")
				--	if points.Value > AmmoCost or points == AmmoCost then    -------- Problem
				--		if (p[i].Character.Torso.Position - script.Parent.Position).magnitude < dist then
				--			GUN.StoredAmmo.Value = GUN.Ammo.Value*10 
				--			GUN.StoredAmmo.Value = GUN.MaxAmmo.Value*10 
				--			GUN.Ammo.Value = GUN.MaxAmmo.Value
				--		end
				--	end 
					GUN.Parent = p[i].Backpack
		        points.Value = points.Value - GunCost 
			end
		end
	end 
end
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked) 

2 comments

0

GUN is not defined, i think

0

You need to identify the Player. You can't just have it right there. It's like telling your dog to fetch a toy you want them to, but they can't because you didn't tell them that it was their favorite. So identify player via Local Player = (player&stuff) and tell me how it goes.