--local InfoBindable = EventsFolder:FindFirstChild("GetInfo")
--local ToClient = EventsFolder:FindFirstChild("SendButtonToClient")

local Notification = EventsFolder:FindFirstChild("Notification")
local EditPlot = EventsFolder:FindFirstChild("EditPlot")

local MarketPlaceService = game:GetService("MarketplaceService")

local OwnerLabel = script.Parent.Parent.Parent.Parent.SignDescription.SurfaceGui.Frame.OwnerLabel

local StarterGui = game:GetService("StarterGui")
local StandNum = script.Parent.Parent.Parent.Parent.Name

local PriceFolder = StarterGui:FindFirstChild("SignPrices")
local UI = PriceFolder[StandNum]

local Template = UI.ScrollingFrame:FindFirstChild("Template")


function Triggered(Player)

	local PlayerName = Player.Name

	if Owner.Value == nil or "NoOne" then -- If booth hasn't been claimed
		if Player:FindFirstChild("Owner") then  -- If Player has claimed one
			Notification:FireClient(Player,"Claimed") -- Let them know they already own one
		else -- If Player hasn't claimed one
			Owner.Value = PlayerName
			OwnerLabel.Text = PlayerName .. "'s Stand"
			Notification:FireClient(Player,true) -- Claimed

			local UserID = game:GetService("Players"):GetUserIdFromNameAsync(PlayerName)
			Bindable:Fire(script.Parent.Parent.Parent.Parent.Name, PlayerName, UserID)

			-- This should be for giving a player a value
			OwnerLabel.Parent.MoneyRaised.Text = Player.leaderstats.Raised.Value .. "$ Raised"

			local OwnerValue = Instance.new("IntValue")
			OwnerValue.Value = script.Parent.Parent.Parent.Parent.Name -- set int value to booth name
			OwnerValue.Name = "Owner"
			OwnerValue.Parent = Player

			-- Would this be the best way to do this? Yes, absolutely!

			-- We want the editPrompt to ONLY show for the client player.

			EditPlot:FireClient(Player, script.Parent.Parent.Parent.Parent.Name, true)
			Prompt.Enabled = false
		end
	end	
end

script.Parent.Triggered:Connect(Triggered)


local Players = game:GetService("Players")
local RemoveButtonsFromClient = EventsFolder:FindFirstChild("RemoveButtonFromClient")

Players.PlayerRemoving:Connect(function(player)
	local plrRemovingName = player.Name
	if plrRemovingName == Owner.Value then
		RemoveButtonsFromClient:FireAllClients(script.Parent.Parent.Parent.Parent.Name)
		Owner.Value = "NoOne"
		EditPlot:FireClient(player, script.Parent.Parent.Parent.Parent.Name, false)
		Prompt.Enabled = true

		OwnerLabel.Text = "No One's Stand"
		script.Parent.Parent.Parent.Parent.SignTitle.SurfaceGui.Frame.SignLabel.Text = "PLS DONATE"
		OwnerLabel.Parent.MoneyRaised.Text = "0$ Raised"

		local Children = StarterGui.SignPrices[script.Parent.Parent.Parent.Parent.Name].ScrollingFrame:GetChildren()
		for _, child in ipairs(Children) do
			if child:IsA("Frame") and child.Name ~= "Template" then
				child:Destroy()
			end
		end
	else
		return -- Not the Owner
	end
end)
