MouseButton1Click in Workspace problem

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)

Help is appreciated!

3 Likes

localscript does not work in the workspace, you must move it to another area that belongs to the player:

1 Like

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

1 Like

I now tried in the client but nothing happens.

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
1 Like

What do you mean by “button UI in the Workspace is pressed”? Are you talking about SurfaceGUI/BillboardGUI buttons?

1 Like

im talking about the surface ui on a block

1 Like

For that, you just have a LocalScript inside PlayerScripts or inside the Player and have a variable to reference the block and its SurfaceGui.

1 Like

I tried already

1 Like

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 "".

1 Like

I tried, still the last function doesn’t work.

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)
1 Like

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

@ElectricPeaPvZ

1 Like

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

how’d you change it to client, i never done that

2 Likes

You’ll need do it in the properties tab (it’s set to Legacy by default):

1 Like

Can i still use localplayer on this and fireserver?

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 :slight_smile::+1:


I forgot to mention that you should also use regular LocalScripts in GUIs which are in StarterGui, due to the same issue mentioned above

1 Like

why have you not tried my solution? :smiling_face_with_tear:
@ElectricPeaPvZ

2 Likes