Script Error with attempting to index nil with 'Parent'

Is there an error in my current script?
I am trying to implement buying teleportation in-game purchase.
This is a normal script in:
image

My output error:
image

My script:

local button = script.Parent
local GUI = script:FindFirstAncestorWhichIsA("ScreenGui")

button.MouseButton1Click:Connect(function(click)
		local plr = game.Players:GetPlayerFromCharacter(click.Parent)
		if plr.leaderstats.Coins.Value >= 120 then
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - 120
			GUI:Destroy()
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.ToPart.Position + Vector3.new(0,4,0))
		end
end)

Thanks for reading! :happy1:

You should be using LocalScripts and RemoteEvents/Functions to achieve this. Using server scripts for client sided buttons makes your job a lot harder than it needs to be.

2 Likes

MouseButton1Click returns nothing, so that’s why the Parent error is happening

Because of this, the script needs to be a localscript to get the player pressing the button if needed, via game:GetService("Players").LocalPlayer), and having a RemoteEvent set up to do the things you want the server to handle from that RemoteEvent, as @ElusiveEpix stated

1 Like