Script only works once

I’m not a very good scripter at all so I might not explain things good enough.
I’m trying to make my own NPC dialogue, and the script works fine, but it only works once.

So how it works is you click on the part that has a script and click detector in it, and once you click the part, it opens up the gui. This is the script inside the part you click:

script.Parent.ClickDetector.MouseClick:Connect(function(player)
player:WaitForChild("PlayerGui").Dialogue.Dialogue_1.Frame.Visible = true
end)

This makes the frame for the dialogue visible, which is what I needed. Now, when the player is done reading what the NPC has to say, they click the button that makes the frame not visible. Here is the script:

script.Parent.MouseButton1Click:connect(function()
	script.Parent.Parent.Visible = false
end)

So everything works fine, unless the player needs to re-read what the NPC had to say. This time, when they click on the NPC for a second time, nothing happens. There is no error in the output, and I can’t figure out why it doesn’t work after the first use. Please tell me what I am doing wrong, and if I didn’t explain something good enough, then please let me know. :happy1:

What is script.Parent.Parent? It seems like you’re making a specific frame visible and turning something else invisible by accident, maybe the main (billboard/surface/screen)gui? I can’t be sure though, so it’d be great to have a bit more context.

2 Likes

Um I don’t know if this would fix it, but why in the world is the GUI located in the Player?? Please put it in the StarterGui, I don’t even know how it works in Player to be honest with you, if that doesn’t work please reply and I’ll try to help with that, have a great day and good luck! :slight_smile:

player.PlayerGui is where the contents of StarterGui are cloned to at runtime. (This happens automatically.)

2 Likes

Here are explorer screenshots:
the part you click
image
the gui
image

1 Like

Hi, as posatta said, what is script.Parent.Parent?

Oh, your issue is that changes don’t replicate from the client to the server. You set Visible to false on the client, but it’s still true on the server. I’d handle the ClickDetector on the client too, if I were you, storing a new LocalScript in StarterPlayerScripts or something instead.

Here’s an explanation of why that happens: Client-Server Runtime | Documentation - Roblox Creator Hub

2 Likes

What do you mean by storing a new LocalScript in StarterPlayerScripts? What will be in the LocalScript?

Move the ClickDetector related code into a LocalScript. (You can’t put it in the same place as the Script because LocalScripts don’t run in the workspace.)

3 Likes

Ok, thanks I got it to work. :happy1: