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

attempt to index function with 'OnServerEvent' fix?

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.

6 comments

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.

0

The first parameter is always the player who fired the event. event2.OnServerEvent:Connect(function(player, ore)

0

not sure if this'll fix your error.

0

On Line 10 try doing-> Connect(function(player, ore)

0

okay thank you

0

i didn't work but thanks for the help

1 answer

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.

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