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

Can Server Scripts access the CurrentCamera and Mouse?

Asked by 7 years ago

So, I'm using a remote event. The localscript fires the server of the event when the player is respawned. It passes the player's mouse, and currentcamera. For some reason I get an error saying that CurrentCamera is a nil value. Can you handle the currentcamera and mouse in a server script?

Here's my RemoteEvent code:

game.ReplicatedStorage.RemoteEvents.Spawn.OnServerEvent:Connect(function(plr, ...)
    local args     = {...}
    local request  = args[1]
    local camera   = args[2]
    local mouse    = args[3]
    local char     = plr.Character
    local humanoid = plr.Character.Humanoid
    local torso

    if request == "Start" then
        local model = game.ServerStorage.Objects.SpawnModel
        local clone = model:Clone()
        clone.Parent = camera

        print(camera.Name) --> This prints nil
        if humanoid.RigType == Enum.HumanoidRigType.R6 then
            torso = plr.Character.Torso
        elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
            torso = plr.Character.HumanoidRootPart
        end



        torso.CFrame = model:FindFirstChild('TeleportPart').CFrame + Vector3.new(0,3,0)
        torso.Anchored = true

        wait()

        if char:FindFirstChild('ForceField') ~= nil then char.ForceField:Destroy() end


    end
end)
1
You can't pass the actual camera object over. Pass any properties you need to access. Goulstem 8144 — 7y
0
Well I need to actually set the parent of model to the camera. Have an idea of how to do this? AstrealDev 728 — 7y

2 answers

Log in to vote
2
Answered by 7 years ago

The answer is "No", or at least "not directly". Certain objects are server-only (ex anything in ServerStorage). Attempting to send a reference to game.ServerStorage.SomeModel in a RemoteEvent will fail (it will be replaced by nil) since the client doesn't have access to it. Similarly, the Mouse and CurrentCamera are client-only, and the same thing happens when sending references to them to the server (as you found out). You can read about the limitations of RemoteEvent here.

As Goulstem says, you need to send the information you need to the server with RemoteEvents or RemoteFunctions. ex, say you want it so that the player's click places a model. Then you would have the client trigger a RemoteEvent on click, sending the relevant coordinates.

I am suspecting that you are trying to create Local Parts. Ideally, if you have the model required in ReplicatedStorage, you can simply have a LocalScript place the model in the workspace (or in the local player's camera). So long as FilteringEnabled is active, no one else will receive these parts.

If you want the model to start on the server and you want to transmit it to a specific client, you can try putting the model in the player's PlayerGui. (I tested this months ago, but not more recently.) If this still works, it will send the model to only the player whose PlayerGui you put it in. Relying on undocumented behaviour like this is risky, though - Roblox might change it later.

A safer alternative: "craftsmashbuild"'s first paragraph's idea is reasonable (with some modifications). Put the model in ReplicatedStorage and then transmit a reference to this model to the client. (It needn't have a unique name or anything since you'll be transferring a reference, not the name.)

A final note: you don't need to use "..." in your function definition. Just use:

game.ReplicatedStorage.RemoteEvents.Spawn.OnServerEvent:Connect(function(plr, request, camera, mouse)

"..." in documentation is just a way of saying "send as many arguments as you like". When you're making your own function, you can use "..." just like you did, but it is easier to just specify the arguments you want. As usual, if not enough arguments are sent, the last ones will receive nil. Only use "..." when you are expecting a variable number of arguments and want them in a list.

0
Well, sounds great. I thought it would be risky trying to do LocalParts with FE on so that's why I used the RemoteEvent. I'll go ahead and attempt this. AstrealDev 728 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

You could try doing something with cloning the item to a random named folder in ReplicatedStorage, then send the name of the folder over to the server to use. (To increase security perhaps?)

Then do whatever you need to the copied objects, and tell the client to replace the old objects with the ones modified by the server sitting in ReplicatedStorage (Assuming you need to update the client) (Also remember to remove the folder when done)

That should hopefully be a partial work around.

Answer this question