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

why does Localscript not edit Server Workspace but Clientside Workspace?

Asked by
CJ4 2
6 years ago

I was making a game where a 3d box moves into an outline and you have to press a certain key to hit the 3d box. It works perfectly using the Play button in roblox studio, but when I use a server, everything just stops working correctly. On the server screen, nothing is moving; but on the client screen, things are moving but input is completely ignored, as if these were two different programs.

Problem I noticed that local scripts only edit Clientside Workspace, which is the workspace that appears for you, which I found odd because usually it does not do this. It works if I change it to a global script, but I still cannot access keyboard input without using a local script. I tried using boolvalues that the local script would set true and the global script would detect so that it would run the functions for them but, as stated earlier, the local script affects the Clientside Workspace not the Server Workspace, so the values appeared true on the client screen but appeared false on the server screen.

Question How do I make functions that affect the Server Workspace when pressing a key on the keyboard?

global script -- used to do global work for localscript

01wait(0.5)
02Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "keyXpressed"
03Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "key0pressed"
04Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "key1pressed"
05Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "key2pressed"
06Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "key3pressed"
07Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "key4pressed"
08Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "key5pressed"
09Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "key1lifted"
10Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "key2lifted"
11Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "key3lifted"
12Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "key4lifted"
13Instance.new("BoolValue", script.Parent.GuitarKeyScript).Name = "key5lifted"
14print("valuesmade")
15while true do
View all 22 lines...

local script -- used to detect key presses on keyboard

01wait(1)
02local mouse = game.Players:FindFirstChild(script.Parent.Name):GetMouse()
03workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
04workspace.CurrentCamera:Interpolate(game.Workspace.Head.CFrame,game.Workspace.Head2.CFrame,0.5)
05script.Parent.Humanoid.WalkSpeed = 0
06local keyX = "j"
07local key0 = "k"
08local key1 = "q"
09local key2 = "w"
10local key3 = "e"
11local key4 = "r"
12local key5 = "t"
13function press(key)
14    key = string.lower(key)
15    if key == keyX then
View all 47 lines...
0
You answered your question in the title! T0XN 276 — 6y

3 answers

Log in to vote
1
Answered by
amanda 1059 Moderation Voter
6 years ago
Edited 6 years ago

ROBLOX has introduced separation between clients and the server as a form of protection from exploiters. In order to get around this, you must use RemoteEvents or RemoteFunctions.

These are Instances that you create and place inside ReplicatedStorage, and it allows a client-side script to communicate with a server-side script, and vice versa.

Here is an article which may help:

Remote Functions and Events

RemoteEvents allow for sending a one-way message, and RemoteFunctions trigger a function on the opposite side and are given a return value.

In this situation, I think we can get away with using a RemoteEvent going from the client to the server, triggering when input is given.

Because the server is the receiver, we will be using the method re:FireServer() to send input and the event re.OnServerEvent to get input, which will be connected to a function. (Note: replace re with a variable containing the path to your RemoteEvent) 0 --Client

01local repstore = game:GetService("ReplicatedStorage")
02local sendkeys = repstore.SendKeys or repstore:WaitForChild("SendKeys") --REMOTE EVENT
03 
04local uis = game:GetService("UserInputService")
05 
06uis.InputBegan:Connect(function(obj)
07    if obj.UserInputType == Enum.UserInputType.Keyboard then
08        sendkeys:FireServer(obj.KeyCode.Name)
09    end
10end)

--Server

1local repstore = game:GetService("ReplicatedStorage")
2local sendkeys = repstore.SendKeys or repstore:WaitForChild("SendKeys") --REMOTE EVENT
3 
4sendkeys.OnServerEvent:Connect(function(player, key)
5    print(player.Name.." has pressed "..key)
6    if key == "G" then
7        print("You are a G ;)")
8    end
9end)

In order to also detect when keys are released, you should use uis.InputEnded which works identical to uis.InputBegan. (Note: uis is referring to UserInputService, to properly define it please reference my Client script above. Here are some more helpful links:

Ad
Log in to vote
0
Answered by
oreoollie 649 Moderation Voter
6 years ago
Edited 6 years ago

A local script runs only on the client. Changes to the client are not replicated to the server unless Filtering Enabled is off. The reason this works in studio play mode is because in play mode, the client is also the server. In server test mode the client and the server are separated. Changes to one client will only affect that one client and not the server or other clients. To answer your question of how to make the client effect the server, you could use a RemoteEvent. I would suggest firing the event every time one of those values changes. On the client, you should call

1event:FireServer()

And the server should wait for this to occur using

1event.OnServerEvent:Connect()

Further documentation on RemoteEvents can be found here. If you need more assistance, feel free to contact me on Discord (Nitrousoxide#3667)

If my answer solved you problem, please remember to mark it as correct.

Log in to vote
0
Answered by 6 years ago

Pressing keys on Local scripts work because local scripts are local, they handle user input. The server doesn’t have a keyboard or mouse, or does it?

No. The clients do have a mouse and keyboard (unless they’re on console or mobile lol).

You use a server script when the changes you want to make are for all the players, and all the server. For example, changing the colour of a part. You’d want the entire server to see it. Not just one player.

Seeing your client code, you’re using Mouse.KeyDown and Mouse.KeyUp, which are deprecated. You’re also using :connect when it’s :Connect. Use UserInputService to get user input. I will provide an example.

Do keep in mind that keys in UserInputService are Enum values, meaning you cannot access it with strings.

If you wanted to get input from user pressing E, you wouldn’t make your variable like this:

local E = "E"

but like this:

local E = Enum.KeyCode.E

1function onkeypress(input, chatting)
2    if chatting then
3        return -- if plr is chatting while pressing our key we will end function
4    end
5 
6    print(input.KeyCode.Name) -- print name
7end
8 
9game:GetService("UserInputService").InputBegan:Connect(onkeypress)

Answer this question