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

For some reason this script says "Attempting to index a nil value", How would I fix this?

.I am making a shop gui for a game and I made a simple button that detects whether or not the player has enough money when they try to purchase an item, and if they do it gives them they item and subtracts the money from their account. The script is very basic but it gives me an error code saying "Attempting to index local "player" (a nil value).

script.Parent.MouseButton1Click:connect(function(player)
	local lstats = player:WaitForChild('leaderstats')
	if lstats.Tokens.Value >= 60 then
		lstats.Tokens.Value = lstats.Tokens.Value - 60
	else
		script.Parent.Text = 'Insufficient Funds!'
	end
end)

I'm tried doing it in both local and global scripts and added a wait feature but neither worked, how would I get this to work in a local script?

3 comments

0

its most likely talking about the (lstats =) part. Paste the whole line from the output and i can help you further.

0

12:54:37.883 - Players.EnderGamer358.PlayerGui.StoreGui.BaseFrame.PurchaseFrame.WeaponsFrame.BasicPistolFrame.Purchase.LocalScript:2: attempt to index local 'player' (a nil value) 12:54:37.885 - Stack Begin 12:54:37.888 - Script 'Players.EnderGamer358.PlayerGui.StoreGui.BaseFrame.PurchaseFrame.WeaponsFrame.BasicPistolFrame.Purchase.LocalScript', Line 2 12:54:37.889 - Stack End

0

Also would changing player to LocalPlayer still give me the player who clicked the button? I tried it in studio and it worked, but I don't know if it would work with other players on the server

1 answer

✓ Accepted answer

The reason that your script broke is that player is not a parameter to the RobloxScriptSignal [event] MouseButton1Click. If this is in a local script, which it should be, because all scripts that work in the PlayerGui, or StarterGui must be LocalScripts due to FilteringEnabled then you would try this method:

local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:connect(function()
	local lstats = player:WaitForChild('leaderstats')
	if lstats.Tokens.Value >= 60 then
		lstats.Tokens.Value = lstats.Tokens.Value - 60
	else
		script.Parent.Text = 'Insufficient Funds!'
	end
end)

But that probably won't work online because of FIltering Enabled. Your best bet is to use this method of using RemoteFunctions

#This is how to use the recommended method

Step 1: put a RemoteFunction in the ReplicatedStorage named HandleTransaction

Step 2: Put these in your game

Replace your script with this LocalScript and put it in the same place.

local player = game.Players.LocalPlayer
local rf = game.ReplicatedStorage:WaitForChild("HandleTransaction")

script.Parent.MouseButton1Click:Connect(function()
	local transaction = rf:InvokeServer()
	if not transaction then
		script.Parent.Text = 'Insufficient Funds!'
	end
end)

Insert a Script inside the ServerScriptStorage

local rf = game.ReplicatedStorage:WaitForChild("HandleTransaction")

function re.OnServerInvoke(player)
	if player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Tokens")
then
		if player.leaderstats.Tokens.Value >= 60 then
			player.leaderstats.Tokens.Value = player.leaderstats.Tokens.Value - 60
			-- if you want something to happen as a result of the transaction, like giving the player an item, put the code here.
			return true
		else
			return false
		end
	end
end

1 comment

0

Thanks a lot, it's working now :)

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