Accessing The Workspace From A Gui

What are you attempting to achieve?
Trying to access a StringValue (Object) in the Game.Workspace via a Gui inside a player’s Gui.

What is the issue?
Works in studio. Doesn’t work in game. I need to have Experimental Mode Off.

What works?
Experimental Mode On.

Could you provide actual code? It sounds like you may need to use WaitForChild but it is a bit hard to tell like this.

local button = script.Parent

button.MouseButton1Click:connect(function()
if game.Workspace.Status.Value == “Ready” then
else
game.Workspace.Status.Value = “Ready”
end
end)

local button = script.Parent
local status = workspace:WaitForChild("Status")

button.MouseButton1Click:connect(function()
if status.Value == “Ready” then
-- idk you do nothing here
else
status.Value = "Ready"
end
end)

See if that makes a difference, otherwise please list the error given in the output here

Uhhhh. Gonna be a no from me here. It has nothing to do with waiting until that comes up. I believe it has to do with a Local Script that is parented in a Gui can’t touch something that is in the workspace, right? No error codes for what I wrote or what you wrote.

When Filtering Enabled is on, you’ll need to use remote events to access PlayerGui. GUI’s are on the client and you are trying to reach it from the server.

1 Like

Well, it’s more I’m trying to reach the Server from the PlayerGui, yeah. I’ll be really honest though in that I don’t understand RemoteEvents what so ever, and the wiki doesn’t quite get me to the point of understanding how to do what I wanna do with one.

You can read the value from the client just fine. You’re trying to change it though, which is a different story. If you’re changing something on the server with a local script, you’ll need to use a remote event.

4 Likes

It would still change it locally though, it depends a bit on the implementation of what happens when it is changed whether that change is properly read. If the server is waiting for a change that would indeed never happen.

Basically with Filtering Enabled, you have to fire remote events between the server and client to communicate. It really just depends on what you are trying to do.

I’m using a script in ServerScriptService to interpret the Status value to decide when to start/end the game. So yea. I need the server.

I’m just trying to get when the Gui button is pressed the StringValue in Workspace to be changed from whatever it is to “Ready” which then my GameScript will interpret and start the game with. Just not sure how exactly firing the events and having things be interpreted and all that works with RemoteEvents.

That is true, but it looks like the value of “Status” might have been changed on the server already, if this existing code isn’t registering. The if statement should still be functioning properly though, just not in the way that might be desired when it comes to changing the value.

It would not error anyway, so whether it ran or not is rather invisible to us.

In terms of remote event setup; use a remote event in a location both client and server can access (such as replicatedstorage), then proceed to fire it from the localscript and listen for OnServerEvent in the server.

Server Script
local Status = workspace:WaitForChild("Status");

local StatusChanger = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage"));
StatusChanger.Name = "StatusChanger";

StatusChanger.OnServerEvent:Connect(function(player)
	Status.Value = "Ready";
	print(player, "changed the value of Status to", Status.Value);
end);
Client Script
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local StatusChanger = ReplicatedStorage:WaitForChild("StatusChanger");

local button = script.Parent;

button.MouseButton1Click:Connect(function()
	StatusChanger:FireServer();
end);

You may want to think about the repercussions of allowing the client to change the value of this object - it will change on the server, which means that it will change for everybody and all players will be able to change this value. If an exploiter or macro user were to spam this event a thousand times, you want your game to be able to handle it well.

Edit: also inb4 someone screams at me for using semicolons. sorry.

4 Likes

Scriptos’ post is a good example of how to connect a remote event. But with remote events, it doesn’t actually sound like you need this value in the workspace to exist.

From what I understand reading through this, all the value was for in the first place was for a script in your gui to tell the server to start the game. That being said, when the server receives the signal from the remote event, it already knows someone is trying to start the game. So, instead of changing a value that will let a different part of the script know to start the game, just run the function that starts the game right there.

To give an example, here’s an edit of Scriptos’ server side script:

local Status = workspace:WaitForChild("Status");

local StatusChanger = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage"));
StatusChanger.Name = "StatusChanger";

local GameInProgress = false

local function StartGame()
	if not GameInProgress then
		GameInProgress = true
		--Put whatever needs to be done to start it here
	end
end

local function EndGame()
	--Do the ending process of the game
	GameInProgress = false
end

StatusChanger.OnServerEvent:Connect(function(player)
	StartGame();
end);

Also if you do change it to do this maybe change the name of the remote event to be something more fitting than ‘StatusChanger’

1 Like

And if you do it this way, you can just check if the game is already started before running the function, that way nothing bad would happen if a macro were to spam click this button a thousand times a second.

2 Likes

Hadn’t considered that. Updated my post to include examples of that too.

1 Like

I’ll be honest my dude, I didn’t write the game script cuz I paid for it close to two years ago (runs great, just I’m basically trying to learn to create a new thing on my own for once…) so I’m just gonna keep with the working stuff and not try to over complicate myself by trying to switch things over and stuff.