Server-Sided input service

I’m new to FilteringEnabled and I have some features in my game that depended on UserInputService which made things appear that everyone was able to see. Come FE, now it’s limited to client-side. Is there a way around this to where I can still have this work and have somewhat of the same system (press ‘r’ for aura)?

My script:
game:GetService(“UserInputService”).InputBegan:connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.R then
script.Parent.Parent.Parent.Character.UpperTorso.Aura.Enabled = true
script.Parent.Parent.Parent.Character.UpperTorso.Aura2.Enabled = true
end
end)

The issue you are experiencing is not due to UserInputService and is actually related to the way that filtering works.

Replication with filtering on is unidirectional, this means that any changes to properties you make on the client are not seen to the server while changes you make on the server are seen by all clients. With filtering off, replication would be bidirectional meaning that properties you change on the client are also seen by the server and other clients.

In your case specifically, when you enable the auras these changes are not visible to the server, hence why only the client can see them. In order to fix this you need to use RemoteEvent’s and RemoteFunction’s. When the client wishes to enable the aura, use a remote to tell the server that the client wishes to enable their aura. It’s worth mentioning that you should also enable it on the client still since it’ll take some time for the information to get to the server and back which means pressing R wouldn’t imeadiately enable the aura on the client if you didn’t.

Your code might look a little something like this.

-- Your clients code
game:GetService(“UserInputService”).InputBegan:connect(function(inputObject, gameProcessedEvent)
  if inputObject.KeyCode == Enum.KeyCode.R then
    local upperTorso = script.Parent.Parent.Parent.Character.UpperTorso
    upperTorso.Aura.Enabled = true
    upperTorso.Aura2.Enabled = true
    game:GetService("ReplicatedStorage").EnableAura:InvokeServer()
  end
end)
-- Then you also need this on the server
game:GetService("ReplicatedStorage").EnableAura.OnServerInvoke = function (player)
  local upperTorso = player.Character.UpperTorso
  upperTorso.Aura.Enabled = true
  upperTorso.Aura2.Enabled = true
end

You’d also need to create a RemoteFunction in ReplicatedStorage called ‘EnableAura’ in order for this to work. I’ve also avoided cleaning up your code in the interests of demonstrating the solution as easily as possible.

Side-note: It is possible for you to disable the aura on the client before the server enables it. Since changes made by the server are shown to all clients the aura will then be re-enabled.

6 Likes

Side note: Don’t use RemoteFunctions for operations that aren’t intended to have a response from the server. A RemoteEvent would suffice for this specific operation.

4 Likes

While you’re correct in saying a RemoteEvent would suffice for the example I have written, I have intentionally used a RemoteFunction since I am not certain whether or not he needs to know once the server has received the request.

Perhaps I should have provided an example using both.

4 Likes

The solution you provided was exactly what I needed. Just for clarification though, if I were to have a system that can change the color of the aura via GUI, would I have to create another RemoteFunction or would I be able to use the same one?

You can use the same Remote function although, as stated above, if you’re not needing to yield for the result from the server, you could just use a RemoteEvent.

What I would suggest for the invoking when changing the color is passing a dictionary like the following;
InvokeServer ({[‘Action’] = ‘ChangeColor’, [‘Color’] = Color3.new ()})

On the server you’ll just check with an if-statement if the action is ChangeColor and then set the Aura to whatever the color is.
OnServerInvoke = function (plr, args)
args [‘Action’] and args [‘Color’]

Much apologies if this is awfully-worded, I’m currently on mobile.

Hope this helps! :slight_smile:

2 Likes

You would be able to use the same one if you give the function arguments.
I am going to keep the same format as how woot3 has done and incorporating CodeSleepRepeat’s example but changing it up a bit.
I’m sure you know how to design and script events for GUI so I will just bind a key to keep this short.

-- Your clients code
game:GetService(“UserInputService”).InputBegan:connect(function(inputObject, gameProcessedEvent)
   if inputObject.KeyCode == Enum.KeyCode.R then
       local upperTorso = script.Parent.Parent.Parent.Character.UpperTorso
       upperTorso.Aura.Enabled = true
       upperTorso.Aura2.Enabled = true
       game:GetService("ReplicatedStorage").EnableAura:InvokeServer({"ToggleRequest"}) -- We request the server to toggle our Aura!
   elseif inputObject.KeyCode == Enum.KeyCode.E then
       local upperTorso = script.Parent.Parent.Parent.Character.UpperTorso
       upperTorso.Aura.Color = Color3.fromRGB(255, 255, 255)
       upperTorso.Aura2.Color = Color3.fromRGB(255, 255, 255)
       game:GetService("ReplicatedStorage").EnableAura:InvokeServer({"ChangeColor", Color3.fromRGB(255, 255, 255)}) -- We want to change the color to pure white!
   end
end)

On the server you will then have to compensate for the argument by adding another value in the function.

-- Then you also need this on the server
game:GetService("ReplicatedStorage").EnableAura.OnServerInvoke = function (player, args)
  if args[1] == ("ToggleRequest") then
    local upperTorso = player.Character.UpperTorso
    upperTorso.Aura.Enabled = true
    upperTorso.Aura2.Enabled = true
  elseif args[1] == ("ChangeColor") and args[2] ~= nil then
    local upperTorso = player.Character.UpperTorso
    upperTorso.Aura.Color = args[2]
    upperTorso.Aura2.Color = args[2]
  end  
 return
end

I haven’t tested this myself and I am working off of little sleep. :slight_smile: As the others has suggested, I would highly recommend using a RemoteEvent if you truly do not need the client to act upon the server’s decision.

2 Likes

You could even improve on this further by sending over a dictionary of properties and values.

-- Client sends this
local changes = {
  Color = Color3.new(0,0,0),
  Enabled = true
}

-- Server runs this
for property, value in pairs(changes) do
  upperTorso.Aura[property] = value
end
3 Likes

Yes, heh. I should’ve included that, but attempting to write code on mobile is not an easy task. Lol :stuck_out_tongue: