Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What Would I Edit In This Script?

This isn't my script, but look at it:

-----------------
--| Constants |--
-----------------

local RANGE = 240
local TIMESTEP = 1 / 30
local SECONDS_BETWEEN_RECONSTRUCTION = 3
local MAX_DIST_RATIO = 0.84

--------------------
--| WaitForChild |--
--------------------

-- Waits for parent.child to exist, then returns it
local function WaitForChild(parent, childName)
	assert(parent, "ERROR: WaitForChild: parent is nil")
	while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
	return parent[childName]
end

-----------------
--| Variables |--
-----------------

local PlayersService = Game:GetService('Players')

local MyPlayer = PlayersService.LocalPlayer
local Camera = Workspace.CurrentCamera


local Radar = WaitForChild(Workspace, 'Radar')

local Radar_Gui = script.Parent
local RadarFrame = WaitForChild(Radar_Gui, 'RadarFrame')

local Background = WaitForChild(RadarFrame, 'Background')
local Border = WaitForChild(RadarFrame, 'Border')
local FoeBlip = WaitForChild(RadarFrame, 'FoeBlip')
local FriendBlip = WaitForChild(RadarFrame, 'FriendBlip')
local SelfBlip = WaitForChild(RadarFrame, 'SelfBlip')

-- Any names (keys) not in this list will be deleted (values must evaluate to true)
local SaveList = {Background = 1, Border = 1, FoeBlip = 1, FriendBlip = 1, SelfBlip = 1}

local ObjectToBlipTable = {}

local LastReconstructionTime = 0

local RunService = Game:GetService('RunService')

--------------------
--| Script Logic |--
--------------------

Background.Visible = true
Border.Visible = true
SelfBlip.Position = UDim2.new(0.5 - SelfBlip.Size.X.Scale / 2, 0, 0.5 - SelfBlip.Size.Y.Scale / 2, 0)

local function IsInRange(inversedFrame, pos)
	local pos = CFrame.new(pos.X, 0, pos.Z)
	local relativeCFrame = inversedFrame * pos
	local distanceRatio = relativeCFrame.p.Magnitude / RANGE
	return distanceRatio < MAX_DIST_RATIO, relativeCFrame
end

local function CheckLos()
	-- Uncomment the following to hide non-visible enemies that are far away
	--[[
	if distanceRatio >= 0.7 and player.TeamColor ~= MyPlayer.TeamColor and player.Character then
		local torso = MyPlayer.Character:FindFirstChild('Torso')
		local otherTorso = player.Character:FindFirstChild('Torso')
		local ray = Ray.new(torso.Position, (otherTorso.Position - torso.Position).unit * RANGE)
		local hit = Workspace:FindPartOnRay(ray, MyPlayer.Character)
		if not hit or hit.Parent ~= otherTorso.Parent then
			unit.Visible = false
		end
	end
	--]]
end

local function ConstructBlipTable()
	for _, object in pairs(Radar:GetChildren()) do
		if object:IsA('ObjectValue') and object.Value and object.Value:IsA('BasePart') then
			local pos = object.Value.CFrame.p
			local objGui
			for _, child in pairs(object:GetChildren()) do
				if child:IsA('Frame') then
					objGui = child:Clone()
					objGui.Parent = RadarFrame
					ObjectToBlipTable[object.Value] = objGui
				end
			end
		end
	end

	for _, player in pairs(PlayersService:GetPlayers()) do
		if MyPlayer and player ~= MyPlayer and player.Character and player.Character:FindFirstChild('Torso') then
			if player.TeamColor == MyPlayer.TeamColor then
				unit = FriendBlip:Clone()
			else
				unit = FoeBlip:Clone()
			end
			unit.Visible = false
			unit.Name = player.Name
			unit.Parent = RadarFrame
			ObjectToBlipTable[player.Character.Torso] = unit
		end
	end
end


local function ReconstructBlipTable()
	-- Destroy anything in the frame that isn't going to be updated
	for _, label in pairs(RadarFrame:GetChildren()) do
		if not SaveList[label.Name] then
			label:Destroy()
		end
	end
	ObjectToBlipTable = {}
	ConstructBlipTable()
end

local function UpdateRadar()
	local frame = Vector3.new(Camera.CoordinateFrame.x, 0, Camera.CoordinateFrame.z)
	local focus = Vector3.new(Camera.Focus.x, 0, Camera.Focus.z)
	local frame = CFrame.new(focus, frame)
	local inversedFrame = frame:inverse()

	for obj, objGui in pairs(ObjectToBlipTable) do
		if obj then
			local pos = obj.CFrame.p
			if pos and objGui then
				local isClose, relativeCFrame = IsInRange(inversedFrame, pos)
				if isClose then
					local xScale = 0.5 - ((relativeCFrame.x / RANGE) / 2) - objGui.Size.X.Scale / 2
					local yScale = 0.5 - ((relativeCFrame.z / RANGE) / 2) - objGui.Size.Y.Scale / 2
					objGui.Position = UDim2.new(xScale, 0, yScale, 0)
					objGui.Visible = true
				else
					objGui.Visible = false
				end
			end
		-- if there is no obj but a gui for it
		elseif objGui then
			objGui.Visible = false
		end
	end
end



while true do
	local now = tick()
	if tick() - LastReconstructionTime > SECONDS_BETWEEN_RECONSTRUCTION then
		ReconstructBlipTable()
		LastReconstructionTime = now
	end
	UpdateRadar()
	wait(TIMESTEP)
end

If I wanted to have this Radar to locate an enemy in the -----------------
--| Constants |--
-----------------

local RANGE = 240
local TIMESTEP = 1 / 30
local SECONDS_BETWEEN_RECONSTRUCTION = 3
local MAX_DIST_RATIO = 0.84

--------------------
--| WaitForChild |--
--------------------

-- Waits for parent.child to exist, then returns it
local function WaitForChild(parent, childName)
	assert(parent, "ERROR: WaitForChild: parent is nil")
	while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
	return parent[childName]
end

-----------------
--| Variables |--
-----------------

local PlayersService = Game:GetService('Players')

local MyPlayer = PlayersService.LocalPlayer
local Camera = Workspace.CurrentCamera


local Radar = WaitForChild(Workspace, 'Radar')

local Radar_Gui = script.Parent
local RadarFrame = WaitForChild(Radar_Gui, 'RadarFrame')

local Background = WaitForChild(RadarFrame, 'Background')
local Border = WaitForChild(RadarFrame, 'Border')
local FoeBlip = WaitForChild(RadarFrame, 'FoeBlip')
local FriendBlip = WaitForChild(RadarFrame, 'FriendBlip')
local SelfBlip = WaitForChild(RadarFrame, 'SelfBlip')

-- Any names (keys) not in this list will be deleted (values must evaluate to true)
local SaveList = {Background = 1, Border = 1, FoeBlip = 1, FriendBlip = 1, SelfBlip = 1}

local ObjectToBlipTable = {}

local LastReconstructionTime = 0

local RunService = Game:GetService('RunService')

--------------------
--| Script Logic |--
--------------------

Background.Visible = true
Border.Visible = true
SelfBlip.Position = UDim2.new(0.5 - SelfBlip.Size.X.Scale / 2, 0, 0.5 - SelfBlip.Size.Y.Scale / 2, 0)

local function IsInRange(inversedFrame, pos)
	local pos = CFrame.new(pos.X, 0, pos.Z)
	local relativeCFrame = inversedFrame * pos
	local distanceRatio = relativeCFrame.p.Magnitude / RANGE
	return distanceRatio < MAX_DIST_RATIO, relativeCFrame
end

local function CheckLos()
	-- Uncomment the following to hide non-visible enemies that are far away
	--[[
	if distanceRatio >= 0.7 and player.TeamColor ~= MyPlayer.TeamColor and player.Character then
		local torso = MyPlayer.Character:FindFirstChild('Torso')
		local otherTorso = player.Character:FindFirstChild('Torso')
		local ray = Ray.new(torso.Position, (otherTorso.Position - torso.Position).unit * RANGE)
		local hit = Workspace:FindPartOnRay(ray, MyPlayer.Character)
		if not hit or hit.Parent ~= otherTorso.Parent then
			unit.Visible = false
		end
	end
	--]]
end

local function ConstructBlipTable()
	for _, object in pairs(Radar:GetChildren()) do
		if object:IsA('ObjectValue') and object.Value and object.Value:IsA('BasePart') then
			local pos = object.Value.CFrame.p
			local objGui
			for _, child in pairs(object:GetChildren()) do
				if child:IsA('Frame') then
					objGui = child:Clone()
					objGui.Parent = RadarFrame
					ObjectToBlipTable[object.Value] = objGui
				end
			end
		end
	end

	for _, player in pairs(PlayersService:GetPlayers()) do
		if MyPlayer and player ~= MyPlayer and player.Character and player.Character:FindFirstChild('Torso') then
			if player.TeamColor == MyPlayer.TeamColor then
				unit = FriendBlip:Clone()
			else
				unit = FoeBlip:Clone()
			end
			unit.Visible = false
			unit.Name = player.Name
			unit.Parent = RadarFrame
			ObjectToBlipTable[player.Character.Torso] = unit
		end
	end
end


local function ReconstructBlipTable()
	-- Destroy anything in the frame that isn't going to be updated
	for _, label in pairs(RadarFrame:GetChildren()) do
		if not SaveList[label.Name] then
			label:Destroy()
		end
	end
	ObjectToBlipTable = {}
	ConstructBlipTable()
end

local function UpdateRadar()
	local frame = Vector3.new(Camera.CoordinateFrame.x, 0, Camera.CoordinateFrame.z)
	local focus = Vector3.new(Camera.Focus.x, 0, Camera.Focus.z)
	local frame = CFrame.new(focus, frame)
	local inversedFrame = frame:inverse()

	for obj, objGui in pairs(ObjectToBlipTable) do
		if obj then
			local pos = obj.CFrame.p
			if pos and objGui then
				local isClose, relativeCFrame = IsInRange(inversedFrame, pos)
				if isClose then
					local xScale = 0.5 - ((relativeCFrame.x / RANGE) / 2) - objGui.Size.X.Scale / 2
					local yScale = 0.5 - ((relativeCFrame.z / RANGE) / 2) - objGui.Size.Y.Scale / 2
					objGui.Position = UDim2.new(xScale, 0, yScale, 0)
					objGui.Visible = true
				else
					objGui.Visible = false
				end
			end
		-- if there is no obj but a gui for it
		elseif objGui then
			objGui.Visible = false
		end
	end
end



while true do
	local now = tick()
	if tick() - LastReconstructionTime > SECONDS_BETWEEN_RECONSTRUCTION then
		ReconstructBlipTable()
		LastReconstructionTime = now
	end
	UpdateRadar()
	wait(TIMESTEP)
end

If I wanted this Radar to locate an enemy in the range(240) of the player, what would I add to make this happen?

3 comments

0

There's already a

0

okay.

0

Woah, thats alot.

1 answer

Maybe not steal other people's scripts and try to fix them?

This discussion is preserved as it was. New answers, comments, and votes are closed.