Attempt to index function with 'OnServerEvent'

I’m a lil confused on what this means. (Server sided)


-- Services & Modules 
local RS = game:GetService("ReplicatedStorage")
local RSS = RS.Storage
local RSE = RSS.RemoteEvents
local StatesFolder = RSE.States

-- Remotes
local TempState = StatesFolder.Temp
local CreateState = StatesFolder.Create
local DestroyState = StatesFolder.Destroy

-- Main Script

TempState.OnServerEvent:Connect(function(Player, State, Duration)
	local Char = Player.Character or Player.CharacterAdded:Wait()
	
	local Bool = Instance.new("BoolValue")
	Bool.Name = State
	Bool.Parent = Char.States
	game.Debris:AddItem(Bool, Duration)
	
end)

CreateState.OnServerEvent:Connect(function(Player, State)
	local Char = Player.Character or Player.CharacterAdded:Wait()
	
	local Bool = Instance.new("BoolValue")
	Bool.Name = State
	Bool.Parent = Char.States
end)

DestroyState.OnServerEvent:Connect(function(Player, State)
	local Char = Player.Character or Player.CharacterAdded:Wait()
	for i,v in pairs(Char.States:GetChildren()) do
		if v.Name == State then
			v:Destroy()
		end
	end
end)


It’s a bit hard to see which line is erroring aswell as what the variables are. Since you don’t give out that information.

You should try rename this to something else as “Destroy” for instance is used to call to Destroy function and it will found Destroy function instead of remote event named “Destroy”

alternatively you can also do

local DestroyState = StatesFolder:FindFirstChild("Destroy") --It will always finding instance

Line 33 is the one that’s bugging out.

Hello, I will try to answer your question, but more details about the part that confuses you would have been helpful.

The code listens for events sent by the client to the server using RemoteEvents. In this case, it allows to create a “states” that are directly added to the player’s character.

1 Like

I renamed the function to “DestroyState” and it started working finely after that.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.