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

Why isn't my string value changing when i set a value?

Asked by 4 years ago

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?

0
use a serverscript WideSteal321 773 — 4y

2 answers

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
4 years ago
Edited 4 years ago

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! :)

0
Thanks for the help. The thing is that the value i am trying to change is in the starter GUI, in a frame of one of the screen guis. I don't think that would be a server value... not sure. But if this value changes, will it only be for the one player, or will it change on all players? Superexperiencedguy 199 — 4y
0
if its changing a value inside each player then you dont need the server script to change that value But you may need it for opening the door/object. I would pass your value through a RemoteFunction then open the door/object using the server script inside the object you're opening/closing TGazza 1336 — 4y
0
if you change that value inside your gui and the gui is in the gui pack then it will only change for the player who changed it. No-one else will see the change.Unless a server script changes all the players Open.Values TGazza 1336 — 4y
0
Yeah that's what i want. Thanks for the help!!! Superexperiencedguy 199 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
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

Answer this question