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

attempt to index function with 'OnServerEvent' fix?

Asked by 4 years ago

I am making a game where players can mine ores and gain money. I'm currently working on the mining part of the game. I have 2 events called Destroy (fired when a player finishes mining an ore) and Hit (fired when a player mines an ore). However, I've run into an error with the script that handles those 2 events. The error is "attempt to index function with 'OnServerEvent'" Here's the code:

local event = game.ReplicatedStorage.Events.Hit
local event2 = game.ReplicatedStorage.Events.Destroy

function frc(model)
    local children = model:GetChildren()
    local child = children[math.random(1, #children)]
    return child
end

event2.OnServerEvent:Connect(function(ore) --Error occurs here
    --ignore everything below this
    local pos
    local rot
    local hits = ore.Hits
    local newDecor

    if ore.Name == 'Ore' then
        newDecor = frc(game.ReplicatedStorage.DecorParts1):Clone()
    elseif ore.Name == 'Ore+' then
        newDecor = frc(game.ReplicatedStorage.DecorParts2):Clone()
    end

    if ore:FindFirstChild('Low') then
        pos = ore.Low.Position
        rot = ore.Low.Orientation
        ore.Low:Destroy()
    elseif ore:FindFirstChild('Medium') then
        pos = ore.Medium.Position
        rot = ore.Medium.Orientation
        ore.Medium:Destroy()
    elseif ore:FindFirstChild('High') then
        pos = ore.High.Position
        rot = ore.High.Orientation
        ore.High:Destroy()
    elseif ore:FindFirstChild('UltraHigh') then
        pos = ore.UltraHigh.Position
        rot = ore.UltraHigh.Orientation
        ore.UltraHigh:Destroy()
    end

    if newDecor.Name == 'Low' then
        ore.OreValue.Value = 5
        hits.Value = 3
        wait(30)
    elseif newDecor.Name == 'Medium' then
        ore.OreValue.Value = 15
        hits.Value = 6
        wait(60)
    elseif newDecor.Name == 'High' then
        ore.OreValue.Value = 35
        hits.Value = 10
        wait(180)
    elseif newDecor.Name == 'UltraHigh' then
        ore.OreValue.Value = 65
        hits.Value = 15
        wait(300)
    end

    newDecor.Parent = ore
    newDecor.Position = pos
    newDecor.Orientation = rot
end)

Please help! This script is critical to the game and I have no idea why the error is occurring. Thanks for taking time to look over my question.

0
If you think something in the area I told you to ignore may be tied to the error, please don't ignore it. Also, this is a Server Script. QuantumPlasmic 84 — 4y
0
The first parameter is always the player who fired the event. event2.OnServerEvent:Connect(function(player, ore) killerbrenden 1537 — 4y
0
not sure if this'll fix your error. killerbrenden 1537 — 4y
0
On Line 10 try doing-> Connect(function(player, ore) bittersweetfate 142 — 4y
View all comments (2 more)
0
okay thank you QuantumPlasmic 84 — 4y
0
i didn't work but thanks for the help QuantumPlasmic 84 — 4y

1 answer

Log in to vote
0
Answered by 3 years ago

I'm pretty sure that it's because your event is name destroy. I've encountered this problem myself and I had used a remote event name destroy.

Ad

Answer this question