I’m trying to make a block where you click and a GUI with text shows, the script seems fine but it doesn’t work. I used this similar script on my other game and it worked but it wasn’t with a function, so for this one, I made it for with a click function and changed it a bit. I don’t really see the problem but struggle to get it to work.
local ScreenGui = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")
debounce = false
script.Parent.ClickDetector.MouseClick:Connect(function()
if not debounce then
debounce = true
local text = "Text"
for i = 1,#text do
ScreenGui.TextLabel.Text = string.sub(text, 1 ,i)
wait()
end
wait(2)
debounce = false
end
end)
Alright so basically you’re using a table operator on a string…
Where you have local text = “Text” and then trying to get the length of it with #text, it won’t work because the # operator is made for tables.
For example
local tab = {1,2,3}
local length_of_Table = #tab
For strings on the other hand, it uses the string library with the .len() function… So instead of #text it’d be string.len(text).
So the complete code would be…
local ScreenGui = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")
debounce = false
script.Parent.ClickDetector.MouseClick:Connect(function()
if not debounce then
debounce = true
local text = "Text"
for i = 1,string.len(text)do
ScreenGui.TextLabel.Text = string.sub(text, 1 ,i)
wait()
end
wait(2)
debounce = false
end
end)
When I click on the block, the GUI doesn’t appear and the output doesn’t say what went wrong.
Somehow this script works:
local ScreenGui = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")
script.Parent.MouseButton1Click:Connect(function()
local text = "Text"
for i = 1,#text do
ScreenGui.TextLabel.Text = string.sub(text, 1 ,i)
wait()
end
This script is for clicking a GUI button and it’s in a textbutton.
That’s probably what is causing it to not work. A localscript will only work if it is a descendant of the player character, or the player’s PlayerGui. There are a few ways of fixing this:
1
The first way is you could run the code entirely from the server side, however it may only work if the contents of the ScreenGui are accessible to the server, such as if it was cloned into the player’s PlayerGui by a server script. If the gui was generated by a local script on the client, it would be invisible to the server.
Server script inside the button (not a localscript):
local debounce = false
local text = "Text"
script.Parent.ClickDetector.MouseClick:Connect(function(player)
local gui = (not debounce) and player.PlayerGui:FindFirstChild("ScreenGui")
if (gui) then
debounce = true
for i = 1, #text do
gui.TextLabel.Text = text:sub(1, i)
wait()
end
wait(2)
debounce = false
end
end)
2
Another way is to use a remote event (this is the preferred option). In this example there would be a RemoteEvent named “GuiTextDisplay” placed in ReplicatedStorage to allow the server to tell the player’s gui to display the text.
Server script in the button:
local debounce = false
local text = "Text"
local event = game.ReplicatedStorage:WaitForChild("GuiTextDisplay")
script.Parent.ClickDetector.MouseClick:Connect(function(player)
if (not debounce) then
debounce = true
event:FireClient(player, text)
wait(2)
debounce = false
end
end)
Local script inside the ScreenGui:
local event = game.ReplicatedStorage:WaitForChild("GuiTextDisplay")
event.OnClientEvent:Connect(function(message)
for i = 1, #message do
script.Parent.TextLabel.Text = message:sub(1, i)
wait()
end
end)