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

6: attempt to index local 'player' (a nil value)?

Script:

local amount = game.StarterGui.MainGui.Buttons["CLICK for CRATE BUX"].amount
local RemoteEvent = Instance.new("RemoteEvent")
RemoteEvent.Parent = game.ReplicatedStorage
RemoteEvent.Name = "Updates"
local function RemoteFunction(player)
	player.leaderstats['Crate Bux'].Value = player.leaderstats['Crate Bux'].Value + amount.Value
end	
RemoteEvent.OnServerEvent:Connect(RemoteFunction())

2 comments

0

You're doing it all wrong

0

How can I fix it?

1 answer

✓ Accepted answer

First of:

1.): Use the player's PlayerGui, not StarterGui.:

What the player sees on their screen are descendants of their PlayerGui. StarterGui holds all guiObjects and other instances that'll be cloned to the player's PlayerGui upon joining.

2.): Don't call the function that's to be connected to an event:

Connections need a function value that isn't supposed to be fired there

What it's supposed to be:

RemoteEvent.OnServerEvent:Connect(RemoteFunction)

Anyways, what you're supposed to do is handle the event from the server, and add a parameter for the amount to be added. Why this you may ask, because the server can't access each client's descendants of their PlayerGui, so we can't get the amount to be added.

Server Code:

local Remote = Instance.new('RemoteEvent')
Remote.Name = 'Updates'
Remote.Parent = game.ReplicatedStorage

function RemoteFunction(player, amount)
   player.leaderstats['Crate Bux'].Value = player.leaderstats['Crate Bux'].Value + amount
end

Remote.OnServerEvent:Connect(RemoteFunction)

Then, we're going to make the client code to fire the event to update the 'Crate Bux' currency

local Remote = game.ReplicatedStorage:WaitForChild('Updates')
local player = game.Players.LocalPlayer

wait()

local amount = player.PlayerGui.MainGui.Buttons['CLICK FOR CRATE BUX'].amount

--When you want to change the currency:
Remote:FireServer(amount.Value)
--When calling FireServer, the player argument isn't needed, because the game already knows what client fired the event

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