MouseButton1Click:Connect(function(player) not getting player

  1. What do you want to achieve? Keep it simple and clear!
    I want to get the player that clicks on a button, to give them an item.
  2. What is the issue? Include screenshots / videos if possible!
    the issue is in a normal script

When I print player from the function it says nil

for i, item in pairs(game.Workspace.House.Shop.SurfaceGui.Frame.NormalShop:GetChildren()) do

local priceofobject
if item:FindFirstChildWhichIsA("TextButton") then
	item:FindFirstChildWhichIsA("TextButton").MouseButton1Click:Connect(function(player) -- player equals nil--
		local price = item:FindFirstChildWhichIsA("TextButton").Name
		if money >= tonumber(price) then
			if item.Name == "Food" then
				local sardines = game.ReplicatedStorage["Can of sardines"]:Clone()
				money -= tonumber(price)
				sardines.Parent = player.Backpack
			end
			game.ReplicatedStorage["Money transfer"]:FireAllClients(money)
		else
			print("You are soo poor")
		end
	end)
end

end

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried placing the code into a local script but the tool that the player receives can only be seen by the player who has the tool.

I have also tried looking on the DevForm

1 Like

When making shop guis, you should always use a local script to detect when the button is pressed, and remote events to do money transfers and item giving.

To get the player in a local script you can do: game.Players.LocalPlayer

2 Likes

Thanks for replying, but i tried using a local script but the item that the player received was only visible to the player who had it. Do remote events work for giving the player an item?

1 Like

Yes they do, you just have to make sure you parent the item to the player.Backpack

2 Likes

Like @domboss37 said, you can use remote events to give the player items on the server side instead of client. You should also do this with the money transfer to lower the chances of something else breaking.

1 Like

Thanks ill go try this now :+1:

1 Like