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

How do I fix this attempt to index local 'event' (a nil value) error?

Asked by 5 years ago
Edited 5 years ago

I am making a C4 tool, when you hold the mouse button in for five seconds it should place the bomb. When it places, it creates a clone of a C4 model that has a script in it that is supposed to start counting down when the remote event activates it. (I'm new to remote events and functions by the way so I don't exactly know what I am doing entirely.)
After it creates the placed c4 model, it is supposed to activate the remote event inside of it and then the C4 tool should disappear. The issue is that it gives me this error:

21:28:10.763 - Players.William12355.Backpack.C4.C4_Script:72: attempt to index local 'plantBombEvent' (a nil value) 21:28:10.763 - Stack Begin 21:28:10.764 - Script 'Players.William12355.Backpack.C4.C4_Script', Line 72 - global plantbomb 21:28:10.764 - Script 'Players.William12355.Backpack.C4.C4_Script', Line 38 21:28:10.765 - Stack End

Here is the script inside the C4 tool:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plantBombEvent = ReplicatedStorage:FindFirstChild("PlantBombEvent")
local Cnplnt =  script.Parent:WaitForChild("CanPlant")
local CanPlant = Cnplnt.Value
local held = false
local timeheld = 0
local UserInputService = game:GetService("UserInputService")
local pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
local Tool = script.Parent
local a = game.Workspace.A_Site

Tool.Equipped:Connect(function(mouse)

end)
Tool.Activated:Connect(function(plantBombEvent)
    CanPlant = CheckCanPlant()
    if CanPlant == false then
        print("cannot plant here")
    else
        held = true 
        while held do 
            wait(1)
            if CanPlant == true then
                timeheld = timeheld + 1 -- adds 1 to ``timeheld`` while held = true
                print(timeheld)
                CanPlant = CheckCanPlant()
                game:GetService("UserInputService").InputEnded:connect(function(input) -- Fires when input ended
                    print("ended")
                    held = false
                    CanPlant = false
                end)
                if timeheld == 5 then
                    plantbomb(plantBombEvent)
                end

            else
                held = false
                timeheld = 0

            end
        end
    end

end)

function CheckCanPlant()
    local magnitude = (a.Position - Tool.Handle.Position).magnitude
    if a.Name == "A_Site" and magnitude < 30.000000000000 then
        CanPlant = true
        return CanPlant
    --elseif a.Name == "B_Site" and magnitude < 100 then
        --script.Parent.CanPlant.Value = true
        --print("Ys")
    else
        CanPlant = false
        return CanPlant
    end
end

function plantbomb(plantBombEvent)
    local plantloc = script.Parent
    plantloc = plantloc.Handle
    local Planted_C4 = game.Lighting.C4_Planted:Clone()
    Planted_C4.Parent = game.Workspace
    Planted_C4:MoveTo(plantloc.Position - Vector3.new(0,3.5,0))
    plantBombEvent:FireServer()
    Tool:Destroy()
    script:Destroy()
end

Here is the script (a normal script) inside the placed C4 model (Which ends up in workspace after cloned and moved)


--Counts down and explodes, destroys itself, and launches the ghost win function when --it reaches 0 local currtime = 10 local C4 = script.Parent local ReplicatedStorage = game:GetService("ReplicatedStorage") local plantBombEvent = Instance.new("RemoteEvent", ReplicatedStorage) plantBombEvent.Name = "PlantBombEvent" local function plantBombFired() print("Yees") while script.Parent.Parent == game.Workspace do wait(1) print(currtime) currtime = currtime - 1 if currtime == 0 then _G.Ghost_Win() C4:Destroy() end end end

The C4 works fine, up until it has to call the remote event.

If someone could explain to me what I am doing wrong, it would be very helpful. I don't know how to reply to comments, I created my account here last night. Also this C4 is not a weapon for a game, it is a part of the game I should say. It is gonna be used like a c4 would in Search in Destroy type game modes.

0
msybe ue a waitforchild instead of findfirstchild. the remote event might not be created before you reference it as a variable. RetroGalacticGamer 331 — 5y
0
I made some grenades and a C4 and it worked. I dont remember exactly how I did it. If no one answers when I get home I will help you. I have a full script. Placing, countdown and exploding. HeyItzDanniee 252 — 5y
0
I replaced the waitforchild with findfirstchild, but now it is saying Infinite yield possible on 'ReplicatedStorage:WaitForChild("PlantBombEvent")' william12355 -5 — 5y

Answer this question