Mutiple pages in my game won't work

I’m still new to the forum so sorry for any mistakes
Anyways, I’m making a slenderman type game where you collect 10 pages.

Now my scripting is kind of bad so it only works with 1 page, So making more then 1 page wouldn’t work.

https://gyazo.com/46777437934da920bad7dd0f517181ed
So here is the folder inside workspace that contains 2 pages for now.

I also tend to grab some code of free models to use in my game wich means i don’t understand everything

Anyways, Here’s the Gui Ignore the collectpage script it works just fine.
https://gyazo.com/656d17faf86fec67e5d334c25fb8656e

Inside the textvisible script is where i want it to detect the pages heres a picture of the textvisible:
https://gyazo.com/f2bd1246b16b8072abc21ee555cba2c4

As you can see only one page works:

Sorry if it’s a simple mistake i’m really new to scripting

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)

May i ask what runservice does?

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)

1 Like

Heartbeat fires every Physics “step”, pretty much when all of the Physics are calculated and shown.

1 Like

You could use a click detector, and a global int value saved in server storage, and add 1 to it everytime you pick up a page.

If you want more info let me know :slight_smile:

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.

Then use a remote event to access server storage

Let me get to my PC and I’ll write you a couple scripts :slight_smile:

I would appreciate that, Thank you

Using E shouldn’t give troubles either if you were to use User Input Service. try taking a look at this page:
https://developer.roblox.com/en-us/api-reference/class/UserInputService

at the bottom is an example regarding the mouse.
to convert it to KeyCode.E isn’t that much work
basically change:

if input.UserInputType == Enum.UserInputType.MouseButton1 then

to

if input.KeyCode== enum.KeyCode.E then

or something along that line

I’ll try it right now, But what’s the diffrence between KeyCode.E and if key == “e”?

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:
https://developer.roblox.com/en-us/api-reference/enum/KeyCode

Oh thanks alot for this page, I’ve actually been looking for these!

And yeah they look alot cleaner.

1 Like

“E” isn’t causing any troubles, It works fine just the text that should apear on all pages don’t work.

You are just checking for one, you need to use for i,v in pairs(folderhere):GetChildren()) do

1 Like

Since i’m really bad at scripting i can’t really understand everything.

Why does this not work?

https://gyazo.com/dba6fa28e80624be36084cc8f29b3f31

(Also someone please tell me how to put the code into a lua format thing)

Because you need to add your own variales, did you changed the folder variable to the folder name in workspace and more?

So local folder = workspace.Pages
Should be
local Pages = workspace.Pages?

Should i make the game for download so you guys can see how it works?

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.