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

LocalScript Issue: The local script works in studio, but not in-game?

I know for a fact this is a 'LocalScript' issue, but I'm not quite sure how to adjust the script for it to function out of studio.

Here's the script:

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


-- 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')


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
				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


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

It's a LocalScript inside a ScreenGUI that's in the StarterGUI. The ScreenGUI also has a frame with ImageLabels in it.

1 answer

✓ Accepted answer

Hi there,

In studio, local scripts work just like normal server scripts, thus, a local script in studio testing would be able to access all the services that would usually only be accessed on a normal server script in game. In your script, you used local Radar = WaitForChild(Workspace, 'Radar') which can only be accessed by a server script. I do suggest you use a remote event for server sided services. You may also want to check to ensure that your script does not access or edit any server sided services.

Your game is filtering enabled. And that’s a good thing! It prevents hackers from accessing the server and changing the properties of the server services. However, that creates some barriers such as stopping server to client communication. If your local script is to get anything other than what it can access in game, it will not work. You may want to check out remote events if you have not read about it: http://wiki.roblox.com/index.php?title=Remote_Events_and_Functions

2 comments

0

Right! Thank you!

0

You're welcome!

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