✓ 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
return true
else
return false
end
end
end
3 comments
its most likely talking about the (lstats =) part. Paste the whole line from the output and i can help you further.
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
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