Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by 5 years ago

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.

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. CodyDev 70 — 5y
1
MouseClick is not responsive to Local Scripts. it must be used on the server Gey4Jesus69 2705 — 5y
1
no, not true, local scripts can listen for the MouseClick event theking48989987 2147 — 5y
0
ok roblox changed that bc the wiki used to explicitly say otherwise. gg Gey4Jesus69 2705 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

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!

0
It says that local player is not a valid member of players. ChasingNachos 133 — 5y
0
sorry i accidentally said LocalPLayer. i edited it Gey4Jesus69 2705 — 5y
Ad

Answer this question