Hello. I would like write a code with this concept: an object size changes depending on how far the player/camera is located from the object
I inserted a localscript inside of the object in workspace and i pasted this code suggested by AI:
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local part = script.Parent
-- Change these numbers to fit your game
local minDistance = 5 -- Closest distance for max size
local maxDistance = 30 -- Farthest distance for normal size
local minSize = Vector3.new(1, 1, 1) -- Size when far away
local maxSize = Vector3.new(5, 5, 5) -- Size when close
RunService.RenderStepped:Connect(function()
local character = player.Character
if not character or not character:FindFirstChild("HumanoidRootPart") then return end
local rootPart = character.HumanoidRootPart
local distance = (rootPart.Position - part.Position).Magnitude
-- Calculate alpha based on distance (0 at maxDistance, 1 at minDistance)
local alpha = math.clamp(1 - ((distance - minDistance) / (maxDistance - minDistance)), 0, 1)
-- Lerp (blend) between the min and max size
part.Size = minSize:Lerp(maxSize, alpha)
end)
But it does not work! I tried even simpler code, like this:
script.Parent.Size = vector3.new(50, 50, 50)
But it does not work! Where did I go wrong? ![]()
