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

This ClickDectector Open GUI script isn't working. Why?

My script is supposed to make a frame visible when someone clicks the part. However, it is not working.

function onClicked() 
    game.StarterGui.PurchaseGui.PurchaseFrame.Visible = true
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

This script is inside the part that should be clicked.

4 comments

1

You're modifying the StarterGui instead of the actual gui the player has. Just use a localscript for the onclicked event, and check if the person who clicked it was the localplayer. If it was, then enable the gui.

1

MouseClick is not responsive to Local Scripts. it must be used on the server

1

no, not true, local scripts can listen for the MouseClick event

0

ok roblox changed that bc the wiki used to explicitly say otherwise. gg

1 answer

✓ Accepted answer

Your script is working exactly as it should. It's updating the Gui in StarterGui. This means that you won't see the change until your character reloads, as the contents of StarterGui are cloned into each player's PlayerGui folder on CharacterAdded. To fix this, all you need to do is get the Gui from the PlayerGui folder using a RemoteEvent, because the server cannot do this.

--Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = Instance.new("RemoteEvent") --create remote
Remote.Parent = ReplicatedStorage

script.Parent.ClickDetector.MouseClick:Connect(function(player)
   Remote:FireClient(player,"") --you can send any data through the second argument
end)

--Local Script, located somewhere like StarterPlayerScripts

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("RemoteEvent") --wait for remote
local player = game:GetService("Players").LocalPlayer

Remote.OnClientEvent:Connect(function(args) --the data you sent
   if player.PlayerGui:FindFirstChild("PurchaseGui") then
      player.PlayerGui.PurchaseGui.PurchaseFrame.Visible = true
   end
end)

Note that Local Scripts will only run when they are or were at some point a direct descendant of the player. This includes places like StarterPack, StarterGui, and StarterPlayerScripts.


##Resources:

Remote Events

Local Script


##Accept and upvote if this helps!

2 comments

0

It says that local player is not a valid member of players.

0

sorry i accidentally said LocalPLayer. i edited it

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