local TouchPart = script.Parent
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local RequiredSHU = 1000000000 -- 1B SHU
local ArenaModel = Workspace:WaitForChild("Arena")
local TpArena = ArenaModel:WaitForChild("TpArena")

-- Create a RemoteEvent for music control
local MusicControlEvent = Instance.new("RemoteEvent")
MusicControlEvent.Name = "MusicControlEvent"
MusicControlEvent.Parent = ReplicatedStorage

local function animateWarning(player, message)
	local playerGui = player:WaitForChild("PlayerGui")
	local warnGui = playerGui:WaitForChild("WarnGui")
	if warnGui then
		local frame = warnGui:FindFirstChild("Frame")
		if frame then
			local textLabel = frame:FindFirstChild("TextLabel")
			if textLabel then
				-- Set the warning message
				textLabel.Text = message

				-- Reset the frame size
				frame.Size = UDim2.new(0, 0, 0, 0)
				frame.Visible = true

				-- Animate the frame
				local targetSize = UDim2.new(0, 361, 0, 64)  -- Assuming this is the original size
				local tweenInfo = TweenInfo.new(
					0.5, -- Time
					Enum.EasingStyle.Bounce, -- Easing style
					Enum.EasingDirection.Out -- Easing direction
				)
				local sizeTween = TweenService:Create(frame, tweenInfo, {Size = targetSize})
				sizeTween:Play()

				-- Play error sound if it exists
				local errorSound = textLabel:FindFirstChild("Error")
				if errorSound and errorSound:IsA("Sound") then
					errorSound:Play()
				end

				-- Close the warning after a delay
				task.delay(3, function()
					local closeTween = TweenService:Create(frame, TweenInfo.new(0.5), {Size = UDim2.new(0, 0, 0, 0)})
					closeTween:Play()
					closeTween.Completed:Wait()
					frame.Visible = false
				end)
			else
				warn("TextLabel not found in WarnGui")
			end
		else
			warn("Frame not found in WarnGui")
		end
	else
		warn("WarnGui not found for player")
	end
end

local function getOrCreateIsInArena(player)
	local isInArena = player:FindFirstChild("IsInArena")
	if not isInArena then
		isInArena = Instance.new("BoolValue")
		isInArena.Name = "IsInArena"
		isInArena.Value = false
		isInArena.Parent = player
	end
	return isInArena
end

local function onTouch(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local shu = leaderstats:FindFirstChild("SHU")
			if shu and shu.Value >= RequiredSHU then
				local character = player.Character
				if character and character:FindFirstChild("HumanoidRootPart") then
					-- Teleport player to arena
					character.HumanoidRootPart.CFrame = TpArena.CFrame

					-- Set IsInArena value
					local isInArena = getOrCreateIsInArena(player)
					if not isInArena.Value then
						isInArena.Value = true
						-- Signal client to change music
						MusicControlEvent:FireClient(player, true)
					end

					-- Set up death listener
					local humanoid = character:FindFirstChild("Humanoid")
					if humanoid then
						humanoid.Died:Connect(function()
							if isInArena.Value then
								isInArena.Value = false
								-- Signal client to change music
								MusicControlEvent:FireClient(player, false)
							end
						end)
					end
				end
			else
				-- Show warning to the player
				animateWarning(player, "You need 1B+ SHU to enter!")
			end
		end
	end
end

TouchPart.Touched:Connect(onTouch)

-- Handle player leaving
Players.PlayerRemoving:Connect(function(player)
	local isInArena = player:FindFirstChild("IsInArena")
	if isInArena then
		isInArena:Destroy()
	end
end) (textlabel is not  being treated as it's suppose to do. animate)