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")
UIS.InputBegan:Connect(function(input, gameProcessed)
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)
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")
UIS.InputBegan:Connect(function(input, gameProcessed)
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)
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
Hope this helps! :)
1 comment
use a serverscript