Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does the script only work half the time?

Asked by
PWNTART 33
9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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)
0
Maybe check if the gun exists? I'm unsure why else it would only sometimes work. magiccube3 115 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

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)

Ad

Answer this question