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

ClcikDetector on sign wont work help?

Ok so im making a game that you can buy houses but I want the forsale sign to open a gui when the click the button please help?

script.Parent.ClickDetector.MouseClick:connect(function()
	local gui = game.StarterGui.HouseBuy.BuyHouse
        gui.Visible = true
end)

it is a simple script But I can't to seem how this works

4 answers

✓ Accepted answer

##Problem

You're trying to change the Visibility value of an object in StarterGui which does not replicate to all players or even a specific player instantaneously.


##Solution

StarterGui is what it is, it's a Starter. Whenever your character respawns it will see the changes. However with your code, you would want to have the Gui show up for a specific person. What the Clicked event provides is a way to get the player object. Whenever a player clicks on a part, then the ClickDetector will get the player who clicked it. You can then use that object and start using the PlayerGui object of all players. PlayerGui is what a player would see on screen, manipulating it for one player will only make it visible to that player. Basically saying that GUI can only be seen by that client.


##Final Script

script.Parent.ClickDetector.MouseClick:connect(function(PlayerWhoClicked) --The MouseClick event will automatically give the player parameter to the function.
    local gui = PlayerWhoClicked.PlayerGui.BuyHouse
    gui.Visible = true
end)

##Warnings

Your code has no protections of it, so if you were to click on the sign and you did not have a PlayerGui object in your player then the script would error. Or even if the names of the objects were changed.


######Hopefully this answered your question. If you have any questions, feel free to comment below.

script.Parent.ClickDetector.MouseClick:connect(function(player)
    local gui = game.ReplicatedStorage.HouseBuy:clone()
	gui.Parent = player.PlayerGui
    gui.BuyHouse.Visible = true
end)

hi i also moved the gui to replicated storage since putting it in startergui isnt really practical, do note that the problem was that your gui wasn being cloned into the players startergui, so he isn't seeing it

script.Parent.ClickDetector.MouseClick:connect(function(player)
    local gui = player.PlayerGui.HouseBuy.BuyHouse
	gui.Visible = true
end)

but if you insist on keeping it in the startergui, you can use the code above instead, it points to the playergui where the gui is kept because of the startergui's function

1 comment

0

im not a professional sorry if i wrote an inefficient code or something

Here:

script.Parent.ClickDetector.MouseClick:connect(function(click) -- Put click here
	if click then   
		 local Click = click.Name
			for i, v in pairs(game.Players:GetChildren())do
				if v:IsA("Player") and v.Name == Click then
					v.PlayerGui.HouseBuy.BuyHouse.Visible = true
				end
			end
	end
end)

You were changing the Visibility of the Gui in StarterGui instead of PlayerGui which is where the Gui would be when you play the game as StarterGui puts all it's children into the Players' PlayerGui.

You can make it like that

function onClicked()
local gui = game.StarterGui.HouseBuy.BuyHouse
gui.Visible = true
end
    script.Parent.ClickDetector.MouseClick:connect(onClicked)

:)

2 comments

0

Your script would not help at all. It would just be the changing of a function from anonymous to not being so. The end user would still receive the exact same end result.

0

okay thank you, I'm still learning how to script :)

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