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

What is wrong with my Spray Bottle Script?

Asked by 9 years ago

Basically, the gear script eliminates every decal one-by-one (I know how to assert, but I see potential problems doing so).

But, now and then, I get a "LocalScript:65: attempt to index field '?' (a nil value)" error while using the gear, and I was wondering if anyone could help me fix this. Thanks in advance.

sp = script.Parent
spraypart = script.Parent.SprayPart


cooldown = 2.1
spraysize = 5
range = 10

local debris = game:GetService("Debris")
equipped = false
check = true

function onEquipped(mouse)
    if spraypart ~= nil and spraypart.Parent ~= nil then
        local weld = Instance.new("Weld")
        weld.Name = "SprayerWeld"
        weld.Part0 = spraypart
        weld.Part1 = sp.Handle

        -- correction term to account for average skew between physics update and heartbeat
        local HitPos = spraypart.Position --+ (-arrow.Velocity * (1/60)) --+ (arrow.CFrame.lookVector * .5)

        local CJ = CFrame.new(HitPos)
        local C0 = spraypart.CFrame:inverse() * CJ
        local C1 = sp.Handle.CFrame:inverse() * CJ

        weld.C0 = C0
        weld.C1 = C1

        weld.Parent = sp.Handle
    end
    equipped = true
    local plr = game.Players.LocalPlayer
    if mouse ~= nil and plr ~= nil then
        mouse.Button1Down:connect(function()

            local chr = sp.Parent
            if chr and check and mouse.Target ~= nil and mouse.TargetSurface ~= nil and sprayid > 0 then
                local surface = mouse.TargetSurface
                local t = chr:FindFirstChild("Torso")
                local h = chr:FindFirstChild("Humanoid")
                local animobject = sp:FindFirstChild("SprayPaint")
                if t and h and animobject then
                    if h.Health > 0 then
                        check = false
                        local anim = h:LoadAnimation(animobject)
                        if anim then
                            anim:Play()
                            sp.Handle.Sound:Play()
                        end
                        if spraypart ~= nil and spraypart.Parent ~= nil then
                            spraypart.SprayEffect.Enabled = true
                        end
                        wait()
                        for i = 1, #mouse.Target:GetChildren() do
                            if mouse.Target:GetChildren()[i]:IsA('Decal') or mouse.Target:GetChildren()[i]:IsA('Texture') then
                                repeat
                                    mouse.Target:GetChildren()[i].Transparency = mouse.Target:GetChildren()[i].Transparency + .1
                                    wait(.1)
                                until mouse.Target:GetChildren()[i].Transparency > 1
                                mouse.Target:GetChildren()[i].Transparency = 1
                                mouse.Target:GetChildren()[i].Texture = 'http://www.roblox.com/images/empty.png'
                            end
                        end
                    end
                    wait(cooldown)
                    if spraypart ~= nil and spraypart.Parent ~= nil then
                        spraypart.SprayEffect.Enabled = false
                    end
                    sp.Handle.Sound:Stop()
                    check = true
                end
            end
        end)
    end
end

function onUnequipped()

    equipped = false
end

sp.Equipped:connect(onEquipped)
sp.Unequipped:connect(onUnequipped)

1 answer

Log in to vote
0
Answered by
Diitto 230 Moderation Voter
9 years ago

Simple, set a variable that is the Mouse target.

Here, at

 repeat
     mouse.Target:GetChildren()[i].Transparency = mouse.Target:GetChildren()[i].Transparency + .1

    wait(.1)

until mouse.Target:GetChildren()

if the user moved their mouse to be over nothing, the script would error, for Mouse.Target would be nil.

If you preemptively set a variable to equal the Mouse's target, then you can prevent this error.

Ad

Answer this question