I want my GUI to play again and again, but the issue I keep running into is that my GUI keeps disappearing and it also doesn’t quit and play again. Here is the code and a video example:
local GUI = game.Players.LocalPlayer.PlayerGui.Text
local tween = game.TweenService:Create(GUI.Text,TweenInfo.new(0.5,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out),{Position = UDim2.new(0.392, 0,0.786, 0)})
local tween2 = game.TweenService:Create(GUI.Text,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.In),{Position = UDim2.new(0.392, 0,0.973, 0)})
game.ReplicatedStorage.TalkText.OnClientEvent:Connect(function(Icon,Text,Sound)
TweenStuff(Icon,Text,Sound)
end)
function TweenStuff(Icon,Text,Sound)
if tween.PlaybackState == Enum.PlaybackState.Playing or tween2.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
tween2:Cancel()
end
GUI.Text.Visible = true
tween:Play()
GUI.Text.ImageLabel.Image = "rbxassetid://"..Icon
GUI.Text.TextLabel.Text = Text
GUI.Sound.SoundId = "rbxassetid://"..Sound
GUI.Sound:Play()
wait(3)
tween2:Play()
wait(2)
GUI.Text.Visible = false
end
The thing I want to happen is that the whole thing starts from square 1. The last function that had been called should have stopped and restarted. I don’t think disabling and enabling will even work. I’ve tried it, but the function doesn’t stop.
Could be wrong here, but Tween:Cancel() stops a tween early and keeps the step at which it was canceled. if you’re using the same UI, you should move the GUI back to the offset that is out the screen, and then tween it. because if you call Tween:Play() after cancelling it when it already finished its ‘animation’ it won’t do anything anymore because the GUI is already in the position. When you call tween:Cancel() make sure you’re also moving it back to its original position.
Because it’s not being sent back to its original position. From my experiences, you also don’t need to cancel a tween, the next one will always override the current tween, but i kept that in because I’m not sure.
local GUI = game.Players.LocalPlayer.PlayerGui.Text
local tween = game.TweenService:Create(GUI.Text,TweenInfo.new(0.5,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out),{Position = UDim2.new(0.392, 0,0.786, 0)})
local tween2 = game.TweenService:Create(GUI.Text,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.In),{Position = UDim2.new(0.392, 0,0.973, 0)})
game.ReplicatedStorage.TalkText.OnClientEvent:Connect(function(Icon,Text,Sound)
TweenStuff(Icon,Text,Sound)
end)
function TweenStuff(Icon,Text,Sound)
if tween.PlaybackState == Enum.PlaybackState.Playing or tween2.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
tween2:Cancel()
GUI.Position = Udim2.new(0,1,0,1) -- set this to whatver you need to, idk what u have it as in the start. This should work! 👍
end
GUI.Text.Visible = true
tween:Play()
GUI.Text.ImageLabel.Image = "rbxassetid://"..Icon
GUI.Text.TextLabel.Text = Text
GUI.Sound.SoundId = "rbxassetid://"..Sound
GUI.Sound:Play()
wait(3)
tween2:Play()
wait(2)
GUI.Text.Visible = false
end
You can just create a debounce to prevent the player from sending the same RemoteEvent until the wait time expires. Just check for the debounce at the beginning of your function there and then set it to false after the wait.
Here’s the thing though, I want it to purposefully restart. Just so if multiple things are said, I can restart the text and say another thing. Maybe this solution you just said is wrong… Or Is It?
Hmmm… I’ve done something similar to this. But it never actually worked out for me. Yes, it should cancel the tweens if they are playing… But this doesn’t reset it’s position and stop it from turning it invisible mid-way. I’ll show of an example of what I mean.
See how if I spam the remoteEvents (The prompts fire the event) it makes the text invisible mid-way through. I’ve got everything else done, I just want to figure out how to get rid of THAT.
your tweens are stacking up and finishing when another is fired, think of it as a queue, youre firing one tween and then another one while the first one is still playing, so it’ll finish the first tweens animation on top of starting the new one, I say just ignore the second function call until tween2 finishes playing to avoid race conditions when event is fired consecutively. And instead of using wait, use .Completed
local isRunning = false
game.ReplicatedStorage.TalkText.OnClientEvent:Connect(function(Icon,Text,Sound)
if isRunning then return end
TweenStuff(Icon,Text,Sound)
end)
function TweenStuff(Icon,Text,Sound)
isRunning = true
GUI.Text.Visible = true
tween:Play()
GUI.Text.ImageLabel.Image = "rbxassetid://"..Icon
GUI.Text.TextLabel.Text = Text
GUI.Sound.SoundId = "rbxassetid://"..Sound
GUI.Sound:Play()
task.wait(3)
tween2:Play()
end
tween2.Completed:Connect(function()
GUI.Text.Visible = false
isRunning = false
end)
This is a bandaid solution, I would recommend you rewrite the function and avoid using wait() (use task.wait() instead because wait() is deprecated) because it yields everything unless running on a different thread.
I took some inspiration from this. I made it so that when the tweens are completed the actions after happens. But I had to remove the debounce on the text, which I don’t want. I’ve created this script,
local GUI = game.Players.LocalPlayer.PlayerGui.Text
local tween = game.TweenService:Create(GUI.Text,TweenInfo.new(0.5,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out),{Position = UDim2.new(0.392, 0,0.786, 0)})
local tween2 = game.TweenService:Create(GUI.Text,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.In),{Position = UDim2.new(0.392, 0,0.973, 0)})
local didSpeak = false
local function TweenStuff(Icon,Text,Sound)
GUI.Text.Position = UDim2.new(0.392, 0,0.973, 0)
print("A")
if didSpeak == true then
print("Erm")
while true do
tween:Cancel()
tween2:Cancel()
didSpeak = false
if didSpeak == false then
break
end
task.wait(0)
end
end
didSpeak = true
GUI.Text.Visible = true
task.wait(0)
tween:Play()
GUI.Text.ImageLabel.Image = "rbxassetid://"..Icon
GUI.Text.TextLabel.Text = Text
GUI.Sound.SoundId = "rbxassetid://"..Sound
GUI.Sound:Play()
tween.Completed:Connect(function()
task.wait(3)
tween2:Play()
end)
tween2.Completed:Connect(function()
GUI.Text.Visible = false
didSpeak = false
end)
end
game.ReplicatedStorage.TalkText.OnClientEvent:Connect(function(Icon,Text,Sound)
TweenStuff(Icon,Text,Sound)
end)
But it only works for about 3 times, then it just bugs out.
Wow careful, youre connecting a signal for .Completed every time the function Is called by the remote event. Move the tween2.Completed and tween.Completed signals out of the function, they don’t need to be in there to work seeing as you instantiated the tweens in a global scope, if you leave it like this it’s gonna cause a pretty big memory leak, and could be the reason it bugs out
local GUI = game.Players.LocalPlayer.PlayerGui.Text
local tween = game.TweenService:Create(GUI.Text,TweenInfo.new(0.5,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out),{Position = UDim2.new(0.392, 0,0.786, 0)})
local tween2 = game.TweenService:Create(GUI.Text,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.In),{Position = UDim2.new(0.392, 0,0.973, 0)})
local didSpeak = false
local function TweenStuff(Icon,Text,Sound)
GUI.Text.Position = UDim2.new(0.392, 0,0.973, 0)
print("A")
if didSpeak == true then
print("Erm")
tween:Cancel()
tween2:Cancel()
didSpeak = false
end
didSpeak = true
GUI.Text.Visible = true
tween:Play()
GUI.Text.ImageLabel.Image = "rbxassetid://"..Icon
GUI.Text.TextLabel.Text = Text
GUI.Sound.SoundId = "rbxassetid://"..Sound
GUI.Sound:Play()
end
tween.Completed:Connect(function()
task.wait(3)
tween2:Play()
end)
tween2.Completed:Connect(function()
GUI.Text.Visible = false
didSpeak = false
end)
game.ReplicatedStorage.TalkText.OnClientEvent:Connect(function(Icon,Text,Sound)
TweenStuff(Icon,Text,Sound)
end)
Also task.wait(0) does nothing so you can remove it. I removed the weird nested while true do loop because it instantly sets did speak to false and then instantly breaks out of it, so a loop is useless there
I think I figured out the mid-invisibility to the gui. Here is the script:
local GUI = game.Players.LocalPlayer.PlayerGui.Text
local tween = game.TweenService:Create(GUI.Text,TweenInfo.new(0.5,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out),{Position = UDim2.new(0.392, 0,0.786, 0)})
local tween2 = game.TweenService:Create(GUI.Text,TweenInfo.new(2,Enum.EasingStyle.Cubic,Enum.EasingDirection.In),{Position = UDim2.new(0.392, 0,0.973, 0)})
local didSpeak = false
local function TweenStuff(Icon,Text,Sound)
GUI.Text.Position = UDim2.new(0.392, 0,0.973, 0)
if didSpeak == true then
GUI.Text.Visible = true
print("No more tweens.")
tween:Cancel()
tween2:Cancel()
didSpeak = false
end
task.wait(0.01)
print("A")
VisiblizeGui()
didSpeak = true
GUI.Text.Visible = true
tween:Play()
GUI.Text.ImageLabel.Image = "rbxassetid://"..Icon
GUI.Text.TextLabel.Text = Text
GUI.Sound.SoundId = "rbxassetid://"..Sound
GUI.Sound:Play()
end
tween.Completed:Connect(function()
task.wait(3)
tween2:Play()
end)
tween2.Completed:Connect(function()
GUI.Text.Visible = false
didSpeak = false
end)
game.ReplicatedStorage.TalkText.OnClientEvent:Connect(function(Icon,Text,Sound)
TweenStuff(Icon,Text,Sound)
end)
function VisiblizeGui()
while true do
GUI.Text.Visible = true
if GUI.Text.Visible == true then
break
end
end
end
It does bug a little, but at least the disappearing stops. The little bugs are the tweens ending too early. Especially the first tween, if the remoteEvent is spammed. But that’s up to me to find out. You all can give advice to help me out, I appreciate the help you all give me.
Also the reason why I use:
function VisiblizeGui()
while true do
GUI.Text.Visible = true
if GUI.Text.Visible == true then
break
end
end
end
Is because I watched the PlayerGui for the Text. The visibility stays off, but this should help the GUI from being visible every time the RemoteEvent is recieved.
local function TweenStuff(Icon,Text,Sound)
GUI.Text.Position = UDim2.new(0.392, 0,0.973, 0)
if didSpeak == true then
DeBug = true
GUI.Text.Visible = true
print("No more tweens.")
tween:Cancel()
tween2:Cancel()
didSpeak = false
end
task.wait(0.01)
print("A")
VisiblizeGui()
didSpeak = true
GUI.Text.Visible = true
tween:Play()
GUI.Text.ImageLabel.Image = "rbxassetid://"..Icon
GUI.Text.TextLabel.Text = Text
GUI.Sound.SoundId = "rbxassetid://"..Sound
GUI.Sound:Play()
end
tween.Completed:Connect(function()
if DeBug == false then
task.wait(3)
tween2:Play()
else
print("oof")
end
end)
tween2.Completed:Connect(function()
GUI.Text.Visible = false
didSpeak = false
DeBug = false
end)
This will fix my bugs. Plus, text is uncommon in the game I’m making so it’s highly unlikely someone will bug it. Text is usually it’s there for character. Thanks for the help!