Is it possible to change this script so that we check the position of the camera in relation to the part

Hello, I would like to know if it is possible to modify the script below to detect the position of the camera instead of the character in relation to the part ? Thank you

local run = game:GetService("RunService")
local players = game:GetService("Players")
local player = players.LocalPlayer

local part = workspace:WaitForChild("Part")

if workspace.StreamingEnabled then
	run.RenderStepped:Connect(function()
		local distance = player:DistanceFromCharacter(part.Position) --Here i need the camera instead of character
		if distance <= workspace.StreamingMinRadius then
			--Do code.
		end
	end)
end

research ToObjectSpace on the developer hub CFrame | Roblox Creator Documentation

If I’m correct, I believe you could just get the positional value of the camera then subtract it from the part’s position the use the magnitude of that vector as your distance.

Example code:



local run = game:GetService("RunService")
local players = game:GetService("Players")
local player = players.LocalPlayer

local camera = workspace.CurrentCamera

local part = workspace:WaitForChild("Part")

if workspace.StreamingEnabled then
	run.RenderStepped:Connect(function()
		local distance = (camera.CFrame.Position-part.Position).Magnitude
		if distance <= workspace.StreamingMinRadius then
			--Do code.
		end
	end)
end

1 Like

Sorry, responded to the wrong person

1 Like

Hello thank you for your answer, i don’t know why but in both script it never detect the position

local run = game:GetService("RunService")
local part = workspace.Simpson:WaitForChild("SimpsonTPFolder")
local part2 = workspace.Flanders:WaitForChild("FlandersTPFolder")
local camera = workspace.CurrentCamera


run.RenderStepped:Connect(function()
	local distance = (camera.CFrame.Position - part.Position).Magnitude
	if distance <= 5 then
		print('hi')
	end
	local distance2 = (camera.CFrame.Position - part2.Position).Magnitude
	if distance2 <= 64 then
		print('hi2')
	end
end)

or

run.RenderStepped:Connect(function()
	local distance = plr:DistanceFromCharacter(part.Position)
	if distance <= 5 then
		print('hi')
	end
	local distance2 = plr:DistanceFromCharacter(part.Position)
	if distance2 <= 64 then
		print('hi2')
	end
end)

I don’t understand why

Try printing the distance and see what it says. It’s probably because neither your player’s character or your player’s current camera is within 64 studs of the part

1 Like

Thank you your script works perfectly

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.