What does attempt to index function with onserverevent mean?

--Services 
local rs =game:GetService("ReplicatedStorage")

--Remote Variables 
local remotesFolder = rs:WaitForChild("Remotes")
local rewardCash = remotesFolder.RewardCash 
print(rewardCash)

local function rewardCash(player, cashamnt)
	local leaderstats = player.leaderstats 
	local cash = leaderstats.Cash 
	cash.Value = cash.Value + cashamnt
end

--Events 
rewardCash.OnServerEvent:Connect(rewardCash)

image

Try doing this:

--Services 
local rs =game:GetService("ReplicatedStorage")

--Remote Variables 
local remotesFolder = rs:WaitForChild("Remotes")
local rewardCash = remotesFolder:WaitForChild("RewardCash") -- wait for child

But I don’t really know.

Edit: Nevermind I was wrong.

1 Like

Rename the function to something else or rename the RewardCash.

2 Likes

You defined two things with the same variable name.

Aside from changing the function name you could also use an anonymous function instead:

--Services 
local rs =game:GetService("ReplicatedStorage")

--Remote Variables 
local remotesFolder = rs:WaitForChild("Remotes")
local rewardCash = remotesFolder.RewardCash 
print(rewardCash)

--Events 
rewardCash.OnServerEvent:Connect(function(player, cashamnt)
	local leaderstats = player.leaderstats 
	local cash = leaderstats.Cash 
	cash.Value = cash.Value + cashamnt
end)

An anonymous function is useful here since it is only being called with this particular event.

On a side note the way this event is structured can lead to security problems in your game, as an exploiter could just fire the event to give themselves cash whenever they want.

1 Like