how to make variable

local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents
local Player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local DefaultSize = script.Parent.Size

function determineClosest(EggsAvailable)
	local CurrentClosest = nil
	local ClosestDistance = script.Parent.MaxMagnitude.Value
	for i,v in pairs(EggsAvailable) do
		local Egg = workspace.Eggs:FindFirstChild(v)
		local mag = (Egg.UIanchor.Position-Player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
		if mag <= ClosestDistance then
			CurrentClosest = Egg
			ClosestDistance = mag
		end
	end
	return CurrentClosest
end

RS.RenderStepped:Connect(function()
	if Player.Character:FindFirstChild("Humanoid") then
		if Player.Character.Humanoid.Health ~= 0 then
			local EggsAvailable = {}
			local CameraRatio = ((Camera.CFrame.Position - Camera.Focus.Position).Magnitude)/11
			script.Parent.Visible = false
			for i,v in pairs(game.Workspace.Eggs:GetChildren()) do
				local mag = (v.UIanchor.Position-Player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
				if mag <= script.Parent.MaxMagnitude.Value then
					EggsAvailable[#EggsAvailable+1] = v.Name
				end
			end
			if #EggsAvailable == 1 then
				local Egg = workspace.Eggs:FindFirstChild(EggsAvailable[1])
				local WSP = game.Workspace.CurrentCamera:WorldToScreenPoint(Egg.UIanchor.Position)
				script.Parent.Visible = true
				script.Parent.Position = UDim2.new(0,WSP.X,0,WSP.Y)
				script.Parent.CurrentTarget.Value = Egg.Name
			elseif #EggsAvailable > 1 then
				local Egg = determineClosest(EggsAvailable)
				local WSP = game.Workspace.CurrentCamera:WorldToScreenPoint(Egg.UIanchor.Position)
				script.Parent.Visible = true
				script.Parent.Position = UDim2.new(0,WSP.X,0,WSP.Y)
				script.Parent.CurrentTarget.Value = Egg.Name
			elseif #EggsAvailable == 0 then
				script.Parent.CurrentTarget.Value = "None"
			end
			script.Parent.Size = UDim2.new(DefaultSize.X.Scale/CameraRatio, DefaultSize.X.Offset, DefaultSize.Y.Scale/CameraRatio, DefaultSize.Y.Offset)
		end
	end
end)

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source