you’ve only applied it to one of the pages. in order to let all objects in the folder react to the distance and such you need to use a for loop. And i also recommend to not use while true for this, instead use the runservice as that’s more reliable.
local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local folder = ... -- change it to the folder location
local Range = ... -- change it to the range variable location.
RunService.RenderStepped:Connect(function(d)
for _,v in pairs(folder:GetChildren())do
local mag = player:DistanceFromCharacter(v.Position)
if mag <= Range.Value then
-- code
else
-- code
end
end
end)
Runservice.RenderStepped executes right before a frame shows. meaning that using it like this makes it more consistent (as wait() is also inconsisten) and less preformance heavy.
Runservice.Heartbeat occurst once every so often (idk much about this one but i do know that renderstepped is often used on the client only)
I used click detector at first and it worked great, But i wanted it to look more like a good scripted game so i thought using “E” to pickup the paper would be way cooler for my game.
Maybe it’s smart to go back to clickdetector since it gave me no problems.
Not alot other than keycode.E being a number inside an enum. enums are baiscally numbers defined with a name which makes code look cleaner. Take a look:
Ah, Slender, haven’t heard that in a while. Believe it or not, the guy still freaks me straight out.
Anyhow… the trick here is to tailor your code to handle arbitrary amounts of pages, meaning that you can add or remove pages and your code will accordingly change based on that value.
There are two parts to this; ensuring that your page-specific code is run across all given pages and then counting incrementally the collected page out of some total number.
In order to ensure that your page code runs across all pages, you require a for loop to run through all the contents of the pages folder. You then need to run your collection function.
local Player = game:GetService("Players").LocalPlayer
local Stepped = game:GetService("RunService").Stepped
local pagesObjects = workspace:WaitForChild("Pages")
local pages = #pagesObjects
local PAGE_RANGE = 15
local function isPageInRange()
local inRange = false
for _, page in ipairs(pagesObjects:GetChildren()) do
-- Need to account for 0 distance, weird behaviour
if distance <= PAGE_RANGE and distance > 0 then
inRange = true
-- Need to explicitly handle so we can early-terminate; no shorthanding
break
end
end
theEarlierBoolValue.Value = inRange
return inRange
end
Stepped:Connect(function ()
local inRange = pageInRange()
if inRange then
-- Set page text to visible
end
end)
From here, it’s just a matter of acting on the nearest page in range upon using a keybind.
A note: contrary to what others are saying, I discourage the use of RenderStepped. It should only be for inexpensive, near-instantaneous operations thst need to go before frames render, such as camera updates or things running beside the camera. This is not one such case where RenderStepped is appropriate.