(Help) There is an error in this script

It is a Shop in Frame, and that when giving to “BUY”, want what it disappeared and that. But I have a problem. Who can help me?

script.Parent.MouseButton1Click:Connect(function(plr)
	local player = plr.Parent:FindFirstChild("Humanoid")
local plr = game.Players:GetPlayerFromCharacter(plr.Parent)
if plr.leaderstats.Food.Value > 0 then 
		plr.leaderstats.Food.Value = plr.leaderstats.Food.Value - 5000
		wait()
		script.Disabled = true
		game.Workspace.Portales.PortalUno.Transparency = 1
		game.Workspace.Portales.PortalUno.CanCollide = false
		game.Workspace.Portales.PortalUno.SurfaceGui.Visible = false
		end
end)

What is the purpose of these lines?

local player = plr.Parent:FindFirstChild("Humanoid")
local plr = game.Players:GetPlayerFromCharacter(plr.Parent)

I’m guessing this is a local script? You can use game:GetService(“Players”).LocalPlayer to have a reference to the current player. From there you should be able to access leaderstats and modify values. The first few lines of your code are likely where the problem is.

.MouseButton1Click does not pass the player as an argument. It’s click detectors that do that (which use the .MouseClick event)

You did not provide much information about the actual error that is occurring and what you’re trying to achieve.

With the minimal information provided, it seems you’re trying to detect MouseButton1Click on a GUI in a server-sided script, which will not work. Along with this, you’re also defining “plr” twice; once in the function and once as an argument of the function called when MouseButton1Click is activated. It should also be noted that “plr” is not a valid argument of the MouseButton1Click event.

If you want to update things such as the leaderstats when a GUI is clicked, you should use a RemoteEvent. RemoteEvents allow LocalScripts to communicate with the server and can be used to make changes on the server. Below is an example of how you can implement a RemoteEvent into your game to make this GUI cause changes on the server.

---> LocalScript inside the button
local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- Make sure you add a RemoteEvent object into ReplicatedStorage, or change this to where you have added it.

script.Parent.MouseButton1Click:Connect(function()
    RemoteEvent:FireServer()
end)
---> Script inside ServerScriptService
local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- Make sure you add a RemoteEvent object into ReplicatedStorage, or change this to where you have added it.

RemoteEvent.OnServerEvent:Connect(function(player)
    if player.leaderstats.Food.Value > 0 then
        player.leaderstats.Food.Value -= 5000
        wait()
        script.Disabled = true -- Not sure why you need to disable the script
        game.Workspace.Portales.PortalUno.Transparency = 1
        game.Workspace.Portales.PortalUno.CanCollide = false
        game.Workspace.Portales.PortalUno.SurfaceGui.Visible = false
    end
end)

Thank you very much for teaching me, I appreciate

1 Like