I have created a local script in Starter GUI. What i want is to change a value that is also in starter gui. But when i run the script it doesn't work. Here is a small part of my script: There are no errors in output, either.
local Open = script.Parent.Parent.Parent.Open Open.Value = "Open"
When i run the code, the string value called "Open" 's Value should be set to open, right? But its not doing that :(
Have i done something wrong?
Yeah you cannot change a server value with a Local script Directly. You need to hook your local script up with Remove Functions/Events then write another script thats on your server that listens to your remove event/functions and your Server script is the one what can change the values.
Heres a couple of Sniplets of code on how to do that, but you would need to work out how to trigger the script. In my example below i've used the mouse click. Not very secure i know but its a working example lol.
Note1: you would need to make a RemoteFunction inside a folder of ReplicatedStorage named RemoteFunctions. This is so the Local script And the server scripts can see this function and comunicate between them!
Code: Local Script (Note the UserInput service isnt required to get this to work its just something im passing to the server as a string input, Kinda like when you hit one of the wasd keys it would send that to your server side value)
local RS = game:GetService("ReplicatedStorage") local RemoteFunctions_Folder = RS.RemoteFunctions local ServerRF = RemoteFunctions_Folder.ChangeMe local UIS = game:GetService("UserInputService") --ServerRF:InvokeServer("Hello World!") UIS.InputBegan:Connect(function(input, gameProcessed) --// Note --// gameProcessed is when the user clicks on a Gui object if they do then this returns true! otherwise its false! if input.UserInputType == Enum.UserInputType.Keyboard then local keyPressed = input.KeyCode ServerRF:InvokeServer("A key is being pushed down! Key:"..tostring(input.KeyCode)..(gameProcessed == true and " || The game engine internally observed this input!" or " || The game engine did not internally observe this input!")) elseif input.UserInputType == Enum.UserInputType.MouseButton1 then ServerRF:InvokeServer("The left mouse button has been pressed down at Vector2.new("..tostring(input.Position).."),"..(gameProcessed == true and " || The game engine internally observed this input!" or " || The game engine did not internally observe this input!")) elseif input.UserInputType == Enum.UserInputType.MouseButton2 then ServerRF:InvokeServer("The right mouse button has been pressed down at Vector2.new("..tostring(input.Position).."),"..(gameProcessed == true and " || The game engine internally observed this input!" or " || The game engine did not internally observe this input!")) elseif input.UserInputType == Enum.UserInputType.Touch then ServerRF:InvokeServer("A touchscreen input has started at Vector2.new("..tostring(input.Position).."),"..(gameProcessed == true and " || The game engine internally observed this input!" or " || The game engine did not internally observe this input!")) elseif input.UserInputType == Enum.UserInputType.Gamepad1 then ServerRF:InvokeServer("A button is being pressed on a gamepad! Button:"..tostring(input.KeyCode)..(gameProcessed == true and " || The game engine internally observed this input!" or " || The game engine did not internally observe this input!")) end end)
And the Server script:
local RS = game:GetService("ReplicatedStorage") local RemoteFunctionsContainer = RS.RemoteFunctions local StringValue = script.Parent.MyValue local RemoteFunction = RemoteFunctionsContainer.ChangeMe RemoteFunction.OnServerInvoke = function(player,Value) --print("Player Named[",player.Name," changed The Value Named [",StringValue.Name,"] to = ",Value) StringValue.Value = player.Name.." Changed The Value with "..Value end local OldValue = "Blah!" while true do local NewValue = StringValue.Value if(OldValue ~= NewValue) then print(NewValue) end OldValue = NewValue wait() end
now when you touch your keys and or move the mouse and click the script changes the string value on the server!
To get your example working through a Local script you would need to do this:
Local Script:
local RS = game:GetService("ReplicatedStorage") local RemoteFunctions_Folder = RS.RemoteFunctions local ServerRF = RemoteFunctions_Folder.ChangeMe local UIS = game:GetService("UserInputService") --ServerRF:InvokeServer("Hello World!") UIS.InputBegan:Connect(function(input, gameProcessed) --// Note --// gameProcessed is when the user clicks on a Gui object if they do then this returns true! otherwise its false! if input.UserInputType == Enum.UserInputType.MouseButton1 then ServerRF:InvokeServer("Open") end end)
Server Script:
local RS = game:GetService("ReplicatedStorage") local RemoteFunctionsContainer = RS.RemoteFunctions local StringValue = script.Parent.MyValue local RemoteFunction = RemoteFunctionsContainer.ChangeMe RemoteFunction.OnServerInvoke = function(player,Value) --print("Player Named[",player.Name," changed The Value Named [",StringValue.Name,"] to = ",Value) StringValue.Value = player.Name.." Changed The Value with "..Value end --// this bit only prints out what the value is so we can see it! local OldValue = "Blah!" while true do local NewValue = StringValue.Value if(OldValue ~= NewValue) then print(NewValue) end OldValue = NewValue wait() end
Hope this helps! :)
for i,v in pairs(game.Players:GetChildren()) v.PlayerGUI.*gui name*.Open.Value = "Open" end
You will need to modify it to fit your needs