[FIXED ]onMouseClick script help!

Hello i want to make a script that when the player click on a button, it print “Button has been clicked!” and after 2 seconds a frame with “Have fun” wrote in it appears while 4seconds. The problem is that when i click on the button the print text appears on the console but the frame with the text doesn’t appears.

local soundbutton = script.Parent
local Fun = game.StarterGui.HaveFuntext

function onClicked()
	print("Button has been clicked !")
	wait(2)
	Fun.Enabled = false
	wait(4)
	Fun.Enabled = true
end

soundbutton.MouseClick:Connect(onClicked)

OnCfflicked

1 Like

This is because of this line:
game.StarterGui.HaveFuntext

Each player has their own PlayerGui and using StarterGui wouldn’t change it for the player.

Assuming you’re using a Script:
Script

local soundbutton = script.Parent

function onClicked(player)
    local Fun = player.PlayerGui.HaveFuntext
	print("Button has been clicked !")
	wait(2)
	Fun.Enabled = false
	wait(4)
	Fun.Enabled = true
end

soundbutton.MouseClick:Connect(onClicked)
1 Like

Yes ! Thank you it works i didn’t tought about this…

But when the frame appears it doesn’t diseappears why ?

1 Like

I’ll fix it for you my dude.

Code:

local Button = script.Parent

Button.MouseClick:Connect(function(Player)
    local Text = Player:FindFirstChildOfClass("PlayerGui"):WaitForChild("HaveFunText")
    print("Button was clicked.")
    wait(2)
    Text.Enabled = true
    wait(4)
    Text.Enabled = false
end)
1 Like

I don’t think there’s any difference between this and @Polyheximal’s script.

Edit: nvm, you switched around the bools

1 Like

@Txppin and @Polyheximal codes doesn’t work…

My bad, use this:

local soundbutton = script.Parent

function onClicked(player)
    local Fun = player.PlayerGui.HaveFuntext
	print("Button has been clicked !")
	wait(2)
	Fun.Enabled = true
	wait(4)
	Fun.Enabled = false
end

soundbutton.MouseClick:Connect(onClicked)

And also @Txppin, please don’t copy the script without making any changes. You’re promoting malpractices such as unnecessary :WaitForChild() usage as well as using unnecessary functions such as :FindFirstChildOfClass(). In this situation, it’s not needed.

2 Likes

We’re assuming that he’s using a ScreenGui here. Visible is not a property of ScreenGui, but Enabled is.

2 Likes

No problem :slight_smile: Thank you for your honest help !

I’m strarting to script so it’s quiet difficult

Is this what you meant xd?

Omg, what are you typing??

1 Like

Just in case you didn’t really understand how StarterGuis and PlayerGuis work.

When you join the game, a copy of everything in the StarterGui is put in your PlayerGui. You can only see and click the stuff in your PlayerGui, not the stuff in the StarterGui. Go test this out for yourself. Playtest, and you will see your player has been added to the Players folder. Now inside your player, there is a folder called “PlayerGui”. If you check in your PlayerGui, you will see a copy of everything in the StarterGui. Now, if you mess around with stuff in StarterGui, you won’t notice any changes. But if you mess around with stuff in your PlayerGui, you will see the changes.