#Text is not appearing when i click something

1 messages · Page 1 of 1 (latest)

topaz thunder
#

my problem is I'm trying to make a page grab mechanic but I'm a baby at luau coding so for some reason the page gets grabbed (destroyed) but the text doesn't change (its nothing by default and is apart of starter player ui)

it would be much appreciated if someone could tell me what i'm doing wrong, as this simple problem has plagued me for like 2 days😭

#

if you guys have any questions i'd be willing to try and answer them

gleaming vault
#

Your code is very unsecure right now. Idk if its the root of your issue but I would def be using "GetService" and "WaitForChild" more

#

Can you try printing the variables you create under the place u make them?

#

That way we can see if that actually is the issue

topaz thunder
#

my only issue is with text changing to what i want it to say, i did make a simple print system in the text script but its not working

#

i should be able to detect when the page is clicked and make the text say what i want it to say

gleaming vault
#

Can you send a video?

topaz thunder
#

okay so when i click on a page part, it disappears, what i want to happen is for the player's gui (pagefoundtext in this case) to say that the page has been found

gleaming vault
#

Can you send the scripts in text rq

#

with codeblock

topaz thunder
#

TextScript

#

local text = game.Players.LocalPlayer.PlayerGui.ScreenGui.PageFoundText
local clickDetector = game.Workspace.Pages.Page1.ClickDetector

clickDetector.MouseClick:Connect(function(player)
text.Text = "You Have Found A Page"
end)

#

DestroyScript

#

local clickDetector = game.Workspace.Pages.Page1.ClickDetector

clickDetector.MouseClick:Connect(function(player)
game.Workspace.Pages.Page1:Destroy()

end)

gleaming vault
#

Can you edit the scripts to include this and send a video please? It can help find the root of the issue.

TextScript

local text = game.Players.LocalPlayer.PlayerGui.ScreenGui.PageFoundText
local clickDetector = game.Workspace.Pages.Page1.ClickDetector

if not text then
  error("ERROR: Text not found in TextScript")
elseif not clickDetector then
  error("ERROR: clickDetector not found in TextScript.")
end

clickDetector.MouseClick:Connect(function(player)
    text.Text = "You Have Found A Page"
end)

Destroy Script

local clickDetector = game.Workspace.Pages.Page1.ClickDetector

if not clickDetector then
  error("ERROR: clickDetector not found in Destroy Script.")
end

clickDetector.MouseClick:Connect(function(player)
    game.Workspace.Pages.Page1:Destroy() 

end)
#

paste these

topaz thunder
#

page 1, destroyscript and text script are all children of "pages" model btw

gleaming vault
#

theyre debugging statements

#

then play the game and tell me if it prints any errors

topaz thunder
#

it didnt print any errors

gleaming vault
#

Okay

#

Is this script local or server

topaz thunder
#

destroy script is a server script and the text script is a local script

#

text script is local so it can interact with the players gui

gleaming vault
#

It can either way

#

Where are they located in explorer

topaz thunder
#

pages (parent of the pages and both scripts) are in workspace

gleaming vault
#

Can u send screenshot

topaz thunder
#

here you go

gleaming vault
#

Yea so that doesnt work

#

local scripts dont run in workspace

#

What you can do is make TextScript a server script with RunContext "Client". See if that works.

Alternatively you are able to call upon the playergui using a server script just fine. You dont need any local scripts for that

#

Also you are stacking 2 mouse click events. One destroys the page and one changes the text

#

I can help you optimize this immensely, but first I want to know: What is your goal with this script?
Imagine im someone who doesnt know anything about what you are making (which is correct), how would you explain the purpose of these scripts to me

topaz thunder
#

it says screengui is not a valid member of playergui

gleaming vault
#

I will help you optimize this script and help you make it work

topaz thunder
tiny pagodaBOT
#

studio** You are now Level 3! **studio

gleaming vault
#

Do you want the page to be removed for every player as soon as its been picked up by one, or only be removed for the player that picked it up?

topaz thunder
#

its a single player game

#

(a slenderman copy to be exact)

gleaming vault
#

So theres only 1 player per server?

topaz thunder
#

yeah

gleaming vault
#

Okay

#

Okay so the current steps I see are:

  1. TextScript makes sure that when the page is clicked, a text pops up to tell the player they picked up the page.
  2. DestroyScript is made so when the page is clicked, it gets destroyed
#

Correct?

topaz thunder
#

theyre seperate scripts so they act in unison be pretty much, yeah

gleaming vault
#

Okay so we are gonna try to make this in one script. Which is possible using a server script.

#

Step one is making your variable creation more secure

#

We are going to be working in your destroy script (Which I would recommend renaming to something like "PageHandler")

#
local clickDetector = game.Workspace.Pages.Page1.ClickDetector

if not clickDetector then
  error("ERROR: clickDetector not found in Destroy Script.")
end

clickDetector.MouseClick:Connect(function(player)
    game.Workspace.Pages.Page1:Destroy() 

end)

So this is the current script.
What I first want to do is change how we obtain the clickDetector. We can simplify this a lot by doing the following:

local pageList = script.Parent
local page1 = pageList:WaitForChild("Page1", 5)

if not page1 then
  error("ERROR: Could not find Page 1.")
end

local clickDetector = page1:FindFirstChild("ClickDetector")

What we do here is the following:

  • First we split up the variables into multiple variables. This makes reusing them easier
  • We use :WaitForChild to make sure it yields until it finds the page, or if 5 seconds have passed. The check after makes sure it errors and stops the script if it didnt find it
  • Then in the final line we just get the clickdetector

Replace your first line of code with this

#

:WaitForChild should be the preferred method when grabbing objects from workspace, since it is more secure (due to it waiting a bit to look for the object)

#

So this is just the setup for getting the click detector

#

read through this and lmk if u have any questions

#

otherwise we'll move on to the next step

gleaming vault
#

@topaz thunder no questions?

topaz thunder
#

nope

#

i understand what the first part of the code is doing pretty well

gleaming vault
#

Alr

#

next chat flood incoming

#

Next we move on to connecting the click to the page and handling code upon click.

local MAX_RETRIES = 7 -- Feel free to change this how u please
local RETRY_INTERVAL = 1 -- Seconds to wait before retrying. Feel free to change
clickDetector.MouseClick:Connect(function(player)
  local CURRENT_RETRIES = 0 -- Current retry Attempt. Dont change
  repeat
    local success, errorMessage = pcall(function()
      local ScreenGui = player.PlayerGui:FindFirstChild("ScreenGui")
      local PageFoundText = ScreenGui:FindFirstChild("PageFoundText")
      PageFoundText.Text = "You have found a page!"
      page1:Destroy()
    end)
  if not success then
    warn(errorMessage)
    task.wait(RETRY_INTERVAL)
  end
  CURRENT_RETRIES += 1
  until success or CURRENT_RETRIES > MAX_RETRIES
end)

So it looks like I do a lot in this script, but a lot of it is just security (preventing errors and stuff). Explanation:

  • First the MAX_RETRIES, RETRY_INTERVAL, and CURRENT_RETRIES are being set for the retrying of the function incase it fails.
  • repeat is used to repeat a function until a certain condition is met. They're pretty similar to while loops. Except in while loops the loop is run until the condition isnt met
  • Inside the repeat we use a pcall function. Pcall is used to securely run code. If it ever errors, it wont break the entire script, it will just stop the running pcall and make success false, and errorMessage the relevant error message. success needs to be true or the MAX_RETRIES count needs to be exceeded for the repeat function to stop.
  • Inside the pcall we run the code for changing the text and destroying the page. Should be self explanatory but lmk if u have questions

After this it should all work. Lmk if u have any questions or need extra explantion on something. I want you to understand everything of the code before you use it.

topaz thunder
#

it works

gleaming vault
#

Sick! Any questions about how something works?

topaz thunder
#

i look at the code some more

#

ill tell you if i dont have question (i understand whats going on)

gleaming vault
#

Alr!

topaz thunder
#

i managed to change it a little bit so that the text waits 4 seconds before disappearing

#

which is nice

gleaming vault
#

Nice! Whatd you do to do that?

topaz thunder
#

made it so that the page is destroyed before the text is changed (if i didnt do this it would wait 4 seconds before disappearing)

gleaming vault
#

Why would it wait 4 seconds before disappearing

topaz thunder
#
local success, errorMessage = pcall(function()
            local ScreenGui = player.PlayerGui:FindFirstChild("ScreenGui")
            local PageFoundText = ScreenGui:FindFirstChild("PageFoundText")
            page1:Destroy()
            PageFoundText.Text = "You have found a page!"
            task.wait(4)
            PageFoundText.Text = ""
        end)
#

i just moved some of the lines in the script

#

and added the second to last 2 lines

gleaming vault
#

It doesnt have to be direcrly after. U could do the following:

.Text = "...."
:Destroy()
task.wait(4)
.Text = ""

Didnt write actual values cause im on phone atm and copy and pasting on phone is pain

topaz thunder
#

oh okay

#

nice to know

gleaming vault
#

all that task.wait does is wait the given seconds before continuing the function.

So imagine u wanted to change the text back on the same frame (AKA without task.wait), you would probs know that u can just place the code that changes the text back anywhere in the script (after the code which sets the text). Task.wait just adds an interval to this

topaz thunder
#

im gonna try and make this work with every page (while being somewhat optimized)
i guess i gotta use tables so im going to make a pagetable (or array? i'm not sure yet)

ive already made the variables that represent all 8 pages

#

btu yeah i dont have questions for the code at this point

#

i'll make another post for anymore issues and change this post to solved, other than that, thank you so much for helping me with this