Plr showing as nil in gui event

Hello! I have been trying to get this simple script to work but there is an issue that I am struggling to resolve. I have searched many websites and found nothing on this issue, including the documentaion on guis. When I printed plr it printed “nil”.

Here is the script.

local BuyButton = script.Parent
local ServerStorage = game:GetService("ServerStorage")
local Apple = ServerStorage.Food.Apple

BuyButton.MouseButton1Click:Connect(function(plr)
	if plr.leaderstats.Money.Value >= 50 then
		plr.leaderstats.Money.Value -= 50
		local ClonedApple = Apple:Clone()
		ClonedApple.Parent = plr.BackPack
	end
end)

My goal is to have the “ClonedApple” to enter the players backpack after they pay. Thank you!

2 Likes

Isnt that already a local script? If it is, you dont need the player variable inside the function, just get the localplayer with game.Players.LocalPlayer

No this is not a local script due to the need to access serverstorage

1 Like

Well the function doesnt give the player as a variable.

You can’t have server scripts controlling UI elements like that. You’ll have to use a local script combined with a remote event.

I have high hopes in this method, but how do I fire the event. I did BuyEvent:FireClient In a local script this did not work. Am I doing this incorrectly?

In a local script, you have to do :FireServer. Then you need to have a server script that is ‘listening’ to the remote event

Same thing came up with BuyEvent:FireServer() any idea why?

What code have you got in your local script and in your server script?

LocalScript:

local BuyButton = script.Parent
local plr = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local BuyEvent = RS.BuyApple

BuyButton.MouseButton1Click:Connect(function()
	BuyButton:FireServer()
end)

ServerScript:

local ServerStorage = game:GetService("ServerStorage")
local Apple = ServerStorage.Food.Apple
local RS = game:GetService("ReplicatedStorage")
local BuyEvent = RS.BuyApple

BuyEvent.OnServerEvent:Connect(function(plr)
	if plr.leaderstats.Money.Value >= 50 then
		plr.leaderstats.Money.Value -= 50
		local ClonedApple = Apple:Clone()
		ClonedApple.Parent = plr.BackPack
	end
end)
1 Like

You should add debounce so the exploiter is unable to spam and the script will not be confused [ suggestion ]

db = false

remote..
if db == true then return end
db = true

-- your script

task.wait(2) 
db = false

It looks like a script error, from the looks of it
you used BuyButton:FireServer() and not BuyEvent:FireServer() in the local script.

1 Like

You should be firing the BuyEvent on the client, not the button

The player’s backpack is spelled “Backpack”, not “BackPack”

yep wrong one shoulda the remote event and not button

This just did absolutely nothing. Although it did def help thanks

1 Like

client

local BuyButton = script.Parent
local plr = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local BuyEvent = RS.BuyApple

BuyButton.MouseButton1Click:Connect(function()
	BuyEvent:FireServer()
end)

S_Side

local ServerStorage = game:GetService("ServerStorage")
local Apple = ServerStorage.Food.Apple
local RS = game:GetService("ReplicatedStorage")
local BuyEvent = RS.BuyApple
local Db = false

BuyEvent.OnServerEvent:Connect(function(plr)
	if Db == true then
		-- do something like fire server to client to ask them to wait or purchase failed
		return
	end

	Db = true
	
	if plr and plr:WaitForChild("leaderstats") then
		
		if plr.leaderstats.Money.Value >= 50 then
			plr.leaderstats.Money.Value -= 50
			local ClonedApple = Apple:Clone()
			ClonedApple.Parent = plr.Backpack
		else print("not enough money") end

		
	else
		warn("Something went wrong :/")
	end


	task.wait(2)
	Db = false
end)
1 Like

@exrfann Same thing happens, no error

Try printing the results to see if it’s actually nil or not.
Replicated this issue in Studio and it works fine for me!

it could be client error , try check remote event , mouse event ?

This might be stupid but make sure there is sufficient money and change plr.BackPack to plr.Backpack when parenting the apple to the player’s backpack