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

Why does the script only work half the time?

works sometimes but other times it doesnt

local gune = game.Lighting.Assault.Assaulta.Guna
script.Parent.MouseButton1Click:connect(function()
		
	    if game.Players.LocalPlayer.leaderstats.KOs.Value >= 0 then
		
		game.Players.LocalPlayer.Backpack.Guna:Destroy()
		game.Players.LocalPlayer.StarterGear.Guna:Destroy()
	
		local clone = gune:Clone()
		clone.Parent = game.Players.LocalPlayer.Backpack
	
		local clonea = gune:Clone()
		clonea.Parent = game.Players.LocalPlayer.StarterGear
		end
end)

1 comment

0

Maybe check if the gun exists? I'm unsure why else it would only sometimes work.

1 answer

✓ Accepted answer

Your problem is you don't check to see if the gun ("Guna") exists before deleting it from the player's backpack or starter gear. The reason it works half of the time is due to the fact that it checks only the backpack to find the gun instead of the player itself. If a player has the gun equipped, then the gun will be relocated to their character model. Here's how you can fix the script:

local gune = game.Lighting.Assault.Assaulta.Guna
local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:connect(function()

	if player.leaderstats.KOs.Value >= 0 then
		if player.Backpack:FindFirstChild("Guna") then
			player.Backpack.Guna:Destroy()
		end

		if player.StarterGear:FindFirstChild("Guna") then
			player.StarterGear.Guna:Destroy()
		end

		local clone = gune:Clone()
		clone.Parent = game.Players.LocalPlayer.Backpack

		local clonea = gune:Clone()
		clonea.Parent = game.Players.LocalPlayer.StarterGear
	end
end)

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