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

Why do i have an "attempt to index nil" error?

Here is my script:


local item1 = game.ReplicatedStorage.remotes.item1

item1.OnServerEvent:Connect(function(amount)
	game.Players.LocalPlayer.itemcounter.DC1.Value = game.Players.LocalPlayer.itemcounter.DC1.Value + amount
end)

I had a remote event fired for this to work:

if DC == "DC1" then
		b1button2.Text = "$"..DC1price
		b3button2.Text = "$"..(DC1price*3)
		confirm.Visible = true
		b1button1.MouseButton1Click:Connect(function()
			if Cash >= DC1price then
				Cash = Cash - DC1price
				print("item bought")
				confirm.Visible = false
				item1:FireServer(1) -- this is the event fired
			end
		end)

The error occurs in the "itemcounter" part, which is a folder that i created through a script in serverscript service, it definately exists, and DC1 is a intvalue also created the sss script.

But itemcounter is aparently a nil value, and its causing an error. Help would be appreciated!!!

2 comments

0

Here is a weird thing for you, when in a local script when any function like MouseButton1Click is called it automaticly get's the player. But you can call the player in the ()'s still. So what is happening is when you fire the event the script is sending 2 pieces of data instead of one. The Player Who Clicked, and your data. When receiving the data you must require 2 pieces of data.

0

yeah i got it thanks :)

1 answer

✓ Accepted answer

Read the error once again: "Attempt to index nil with 'itemcounter'".

This means that the Instance you're trying to reference itemcounter from doesn't exist.

So why is it that LocalPlayer doesn't exist? This is due to the fact that the variable is a pointer to the Client's affiliated Player Object. Each Client (connected user) has their own Player, which is locally tracked as LocalPlayer, this is why you can refer to yourself from LocalScripts—these programs run on your device. The Server however, is an architecture that is designed to serve Clients, not be one; the Server isn't an active user, therefore doesn't have its own Player.


Fortunately, the OnServerEvent RBXScriptSignal will always first pass the Remote's caller as an argument to your Callback. You can use this to refer to who invoked the RemoteEvent:

local item1 = game.ReplicatedStorage.remotes.item1

item1.OnServerEvent:Connect(function(Player, Amount)
	Player.itemcounter.DC1.Value += Amount --// Addition shorthand for simplicity.
end)

####Note:

Due to the fact that this Signal will always pass a Player Object first, the first parameter will refer to it; in your previous code, "amount" would have been the caller's Player.

P.S. I highly advise you do not check or perform currency operations on the Client, as that will not be reliably accurate or replicate. The current system is extremely susceptible to exploitation and is poorly designed, you're bound to experience issues in the future.

2 comments

0

Ok thanks it works now. Just a quick question, how would i perform the currency operation on the client otherwise?

0

You do them on the Server.

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