Attempt To Index Nil With With 'OnServerEvent'

15:24:13.458 - ServerScriptService.Upgrades:1: attempt to index nil with ‘OnServerEvent’

Event.OnServerEvent:Connect(function(player)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local upgrades = game.Players.LocalPlayer.PlayerScripts:WaitForChild('UpgradesIntValue')

local increase = 1.5
local price = upgrades.Value*increase

	

if player.leaderstats.Elixir.Value >= price then
	player.leaderstats.Elixir.Value = player.leaderstats.Elixir.Value - price
	
	upgrades.Value = upgrades.Value + 1
	return true 
else
		return false
end

end) 

Thanks

What is Event?

Edit: I also noticed that you’re using LocalPlayer in a server script. This will be nil, because LocalPlayer is the pllayer that LocalScripts run for, and you have a player parameter in OnServerEvent, so use that instead of LocalPlayer.

1 Like
local Event = locationtoeventparent:WaitForChild("RemoteFunction");

Event.OnServerEvent:Connect(function(player)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local upgrades = game.Players.LocalPlayer.PlayerScripts:WaitForChild('UpgradesIntValue')

local increase = 1.5
local price = upgrades.Value*increase

	

if player.leaderstats.Elixir.Value >= price then
	player.leaderstats.Elixir.Value = player.leaderstats.Elixir.Value - price
	
	upgrades.Value = upgrades.Value + 1
	return true 
else
		return false
end

end)

try that, your event did not look defined, which is what is causing your error.

1 Like

Why are you using game.Players.Localplayer when you are in the server?
Player is already defined use player

1 Like

As GalaxyGourmet said you’re going to still run into issues due to that local player. You need to instead change that to the Player you have passed via the remote in the function.

local upgrades = game.Players.LocalPlayer.PlayerScripts:WaitForChild('UpgradesIntValue')

would become

local upgrades = game.Players:FindFirstChild(player.Name).PlayerScripts:WaitForChild('UpgradesIntValue')
1 Like

Thanks, but this line isn’t working now

local Event = OnServerEvent:Connect(function(player)

16:10:22.570 - ServerScriptService.Upgrades:3: attempt to index nil with ‘Connect’

You aren’t suppose to localise the function. The Event is an actual Remote Event where you define its location.
Example:
local Event = game.ReplicatedStorage.RemoteEvent
Event.OnServerEvent:Connect(function(player)
–Do something here
end)

1 Like

What is player scripts?
16:32:26.762 - PlayerScripts is not a valid member of Player

PlayerScripts only shows for the client.