I want to fire an event when a button UI in the Workspace is pressed. However, when it is pressed, the button doesn’t run. I looked over Developer Forum for something that could help me which did, but not this one. Here’s my LocalScript:
local model = script.Parent.Parent.Parent.Parent
script.Parent.MouseButton1Click:Connect(function() -- doesnt run
local plr = game:GetService("Players").LocalPlayer
if plr then
game.ReplicatedStorage.Events.Play:FireServer(plr, model)
end
end)
LocalScripts, aside from one exception, do not run under the “Workspace” container. To learn more about where you can place this type of script, visit this page. Any UI that a client interacts with should be scripted on the client, so you’re halfway there. When you need to script UI in the 3D space, you’ll continue to do so under StarterGui. Move your SurfaceGui or BillboardGui into StarterGui, and set its “Adornee” property to the BasePart it was originally under. Finally, adjust the pathways of the instances you were referencing before
local plr = game.Players.LocalPlayer
while plr.Board.Value == "" do
wait(0.1)
if plr.Board.Value ~= "" then
game.Workspace.Boards[plr.Board.Value].StartButton.SurfaceGui.TextButton.MouseButton1Click:Connect(function()
local model = game.Workspace.Boards[plr.Board.Value]
game.ReplicatedStorage.Events.Play:FireServer(plr, model)
end)
end
end
In LocalScripts, you should generally always use WaitForChild because it takes time for things to load on the client. It’s likely that player.Board still hasn’t loaded and therefore, the code won’t run because the value is nil, while the statement is only checking whether its "".
local plr = game.Players.LocalPlayer
local r = false
while wait(0.01) do --plr:WaitForChild("Board").Value == "" do
if plr:WaitForChild("Board").Value ~= "" then
r = true
end
end
game.Workspace:WaitForChild("Boards")[plr:WaitForChild("Board").Value]:WaitForChild("StartButton"):WaitForChild("SurfaceGui"):WaitForChild("TextButton").MouseButton1Click:Connect(function()
if r == true then
local model = game.Workspace.Boards[plr:WaitForChild("Board").Value]
game.ReplicatedStorage.Events.Play:FireServer(plr, model)
end
end)
add all components inside surface or billboard gui and place that billboard gui inside the starter gui.
then set adornee to the part you wish.
and it should work fine
Instead of a LocalScript, use a regular Script, but change its RunContext to Client. This would make it run on the client-side, like a LocalScript, without the drawback of being unable to run in Workspace
As I said, it will work like a LocalScript would, so you’ll be able to use both LocalPlayer and FireServer
A word of warning though, you should still use actual LocalScripts for Tools, scripts in StarterCharacterScripts, and scripts in StarterPlayerScripts, otherwise you’ll run into issues with the script running before it’s placed in the “correct” location
Using Scripts with RunContext set to Client in Workspace is perfectly safe, though
I forgot to mention that you should also use regular LocalScripts in GUIs which are in StarterGui, due to the same issue mentioned above