Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make this textlabel not glitch when a player touches a block?

So I made a script where when a player touches a block a textlabel appears, it works perfectly fine except that it glitches because the player touches it to many times, even though they touch it once the output still says Player Touched(x12) so that means the player touched it 12 times.

Sorry if that is hard to understand, I am not good at explaining things.

server script:

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")then
game.ReplicatedStorage.Popup2:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
end
end)

local script: (makes the textlabel appear)

local playername = game.Players.LocalPlayer.Name
game.ReplicatedStorage.Popup2.OnClientEvent:Connect(function()
 game.Players.LocalPlayer.PlayerGui.PlayGui.TextLabel.Visible = true
wait(0.1)
sentences = {
    "I remember this place.. It is my home?",
    "I remember a bit of that day in the lab.... I was looking at a scientist.",
    "WHAT EVER IS HAPPENING NEEDS TO STOP!!!"
    
    
}


--Displays current sentence with type writer effect
function displaySentence(sentence)
    for i = 1, #sentence do
        game.Players.LocalPlayer.PlayerGui.PlayGui.TextLabel.Text = string.sub(sentence, 1, i)
        wait(0.1) -- Speed of the text Typewriter animation
    end
end


--loop thru all the sentences, displaying each sentence 
--with a 1 second pause in between each sentence
for _, sentence in pairs(sentences) do
    displaySentence(sentence)
    wait(1)
end
wait(3)
script.Parent.TextLabel.Visible = false
end)

local script: (destroys the block afterward)

game.ReplicatedStorage.Popup2.OnClientEvent:Connect(function()
print ("Player Touched")
	game.Workspace.TouchPart2:Destroy()
end)

1 comment

0

Add a debounce

1 answer

✓ Accepted answer

You can delete the block as soon as you touch it.

You can also add a variable that prevents a player from viewing the same thing twice. Something like:

local playername = game.Players.LocalPlayer.Name

local debounce = false

game.ReplicatedStorage.Popup2.OnClientEvent:Connect(function()
	if debounce then return end --Stops if debounce is already true
	debounce = true
	game.Players.LocalPlayer.PlayerGui.PlayGui.TextLabel.Visible = true
	wait(0.1)
	sentences = {
		"I remember this place.. It is my home?",
		"I remember a bit of that day in the lab.... I was looking at a scientist.",
		"WHAT EVER IS HAPPENING NEEDS TO STOP!!!"
		}


	--Displays current sentence with type writer effect
	function displaySentence(sentence)
		for i = 1, #sentence do
			game.Players.LocalPlayer.PlayerGui.PlayGui.TextLabel.Text = string.sub(sentence, 1, i)
			wait(0.1) -- Speed of the text Typewriter animation
		end
	end


	--loop thru all the sentences, displaying each sentence 
	--with a 1 second pause in between each sentence
	for _, sentence in pairs(sentences) do
		displaySentence(sentence)
		wait(1)
	end
	wait(3)
	script.Parent.TextLabel.Visible = false
end)

I apologize for fixing your spacing.

1 comment

0

Tysm!

This discussion is preserved as it was. New answers, comments, and votes are closed.