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

How do I make a server sided camera shake?

Asked by
ede2355 22
4 years ago

By server sided I mean how can I make the camera shake for everyone in the game when a specific function happens, I've already attempted to use OnClientEvent() and FireAllClients() to achieve this but to no avail, but I'm not 100% sure what I'm doing wrong if I am doing it wrong.

Any help will be much appreciated.

Here is the code I have so far:

--//Server Script

    local players = game.Players:GetChildren()
    local remote = game.ReplicatedStorage.Shake
    for i,v in pairs(players) do
        local char = v.Character
        local Humanoid = char:WaitForChild("Humanoid")
        remote:FireAllClients(Humanoid)
    end
--//Local Script
game.ReplicatedStorage.RockShake.OnClientEvent:Connect(function(humanoid)

            for i = 1, 20 do
    local x = math.random(-100,100)/100
    local y = math.random(-100,100)/100
    local z = math.random(-100,100)/100
    humanoid.CameraOffset = Vector3.new(x,y,z)
    wait()
end
end)

1 answer

Log in to vote
0
Answered by
Sparks 534 Moderation Voter
4 years ago
Edited 4 years ago

You are indexing two different RemoteEvents from ReplicatedStorage.

In your LocalScript, you are listening to "RockShake," but on the server you are calling FireAllClients() on a remote called "Shake." I am not sure if you had your output open when you were testing.

Besides that, it seems you are using FireAllClients incorrectly. If the client listens to the event, you don't need to pass their humanoid in as an argument. Just use the client's humanoid. You also do not need to loop through every player to FireAllClients. By doing so, you will fire all clients multiple times, which I assume is not what you were trying to do.

--//Server Script

    local players = game.Players:GetChildren()
    local remote = game.ReplicatedStorage.Shake --Change as necessary
    remote:FireAllClients()

--//Local Script

game.ReplicatedStorage.Shake.OnClientEvent:Connect(function()
    local humanoid = game.Players.LocalPlayer.Character.Humanoid
    for i = 1, 20 do
        local x = math.random(-100,100)/100
        local y = math.random(-100,100)/100
        local z = math.random(-100,100)/100
        humanoid.CameraOffset = Vector3.new(x,y,z)
        wait()
    end
end)
0
I also forgot to mention, it only works for me but no one else, I was also reported it worked for like a split second. ede2355 22 — 4y
0
But thank you very much, I will attempt this tomorrow since I have to go somewhere now. ede2355 22 — 4y
0
I tested it and it seemed to be working for about 1-2 seconds. If you want it to be longer then you need to set a bound higher than 20. Sparks 534 — 4y
0
That much I know, but I’m not 100% sure it will work for everyone else in the game. Are you able to access all of the clients’ humanoids using FireAllClients and OnClientEvent? ede2355 22 — 4y
View all comments (7 more)
0
I don't think I understand what you are saying very well. When you are doing FireAllClients(), each player connects the shake function to the OnClientEvent, meaning everyone will shake their own screens. Is there something else you were trying to do? Sparks 534 — 4y
0
Nope, that’s all I wanted to do, I was just wondering if the parameter for the FireAllClient() were all of the players. Thanks for clearing up my confusion. ede2355 22 — 4y
0
Oh okay lol. Yeah, FireAllClients() will fire the event for all clients, meaning each client will connect that function to the listener at the same time. Sparks 534 — 4y
0
Alright, thanks a bunch, I tried looking for this somewhere but wasn't completely certain. ede2355 22 — 4y
0
I've just ran into a predicament , I tried indexing character using players as the parameter and it says it is attempting to index nil, what I'm trying to do is get EVERYONES character and humanoid, and apply the cameraoffset to all of them. It's just I can't reference every ones humanoid which is needed for this specific script. ede2355 22 — 4y
0
Camera effects should usually be done on the client, as the camera is unique to each client. Why are you trying to get everyone's humanoid instead of firing all clients? Sparks 534 — 4y
0
Because I need to affect all of the humanoid's CameraOffset so it happens to everyone at once. For example, if I was to throw a projectile, and it hit something , I would want the screen to shake for everyone, also when I fire all clients, as you can see in my script, I need to reference the humanoid but I can't even reference the player to even get to the humanoid, how else would I do it? ede2355 22 — 4y
Ad

Answer this question