Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why are my scripts running poorly in-game than they are in studio?

1 answer

✓ Accepted answer

When testing in-studio, communication with RemoteEvents to the client are almost instant. Once you start getting in-game, it is much slower. I'm assuming you are rendering this on the server side.

Solution

In order to fix this "delay", you have to do the rendering client-side. We want the LocalPlayer to feel as if their actions are instantaneous. It will render on their screen perfectly, then tell the server that it did an action. Now the server has a couple of options.

The server can either render it and make the player who requested to render it not display it (deleting or setting transparency).

The other route is to have the server :FireClient() to the other players and have them render it on their screens.

This will make it delayed for everyone else, but instant for the player who ran it. Good luck, those effects look pretty cool.

Edit: Here is a script explaining what to do.

LocalScript

RemoteEvent.OnClientEvent:Connect(function(arg1)
    local character_who_FireServer = arg1
    local referencePoint = character_who_FireServer.Head.Position
end)

ServerScript

RemoteEvent.OnServerEvent:Connect(function(player, arg1)
    local player_who_FireServer = player
    --This event will always tell you the PlayerObject
    --that fired the remote (first parameter).
    local character_who_FireServer = arg1
    for i,v in next, game.Players:GetPlayers() do
	    if (v ~= player_who_FireServer) then
		    RemoteEvent:FireClient(v, character_who_FireServer)
	    end
    end
end)

Now once you :FireServer(), you have to provide arguments (arg1) to send in this case we need a reference point.

local arg1 = LocalPlayer.Character
RemoteEvent:FireServer(arg1)

See how arg1 travels to the ServerScript. The ServerScript then loops through all the players except the player who fired the event. The rest of the clients will receive the information with the Character object the LocalPlayer fired. Now you can use the LocalPlayer's Character as a reference point for all the other clients.

9 comments

0

Do you know of a way to access certain references that would be easy in the server script, but harder on the receiving end of a local script ( :FireAllClients() )? By this I mean the original player, because I would have to change the position of these lightning bolts based on the original player's character.

0

You could use AnimationTrack.KeyframeReached to sync the animation with the lightning effect. If the animation is played on the server as well as the lightning bolts, you can sync it pretty well. Just know the client will be delayed when it triggers the ability, but you can always cover it up (with sounds, animations, camera shakes, etc). Hope I understood what you were asking.

0

Yeah I understand that, it was something I tried out today. Do you know how to transfer / get parameters or like references? What I mean is like the character of the player, cause for me to do a FireAllClients, each client would have to have the same local script available, but I do not know how to make a variable that is referencing only to one player, so that I may create instances based on that

0

like how I can say local Character = Player.Character or Player.CharacterAdded:Wait(), but in the local script for the FireAllClients() how would I reference back to this original character from the server script?

0

So the LocalPlayer will :FireServer(Character) and the server will loop through all the players and :FireClient(otherPlayer, Character), but if otherPlayer is equal to LocalPlayer then don't fire/send. The otherPlayer will have an OnClientEvent receiving it and use the sent Character as the reference point. When you fire a remote, you can send objects, so the other clients can receive the referenc

0

I updated my answer explaining more about what you questioned. The names of the variables and the arguments don't have to be the same, just for explanation purpose. They do have to be in the correct positions though.

0

thank you :)

0

so if I did like for i,v in pairs(game.Players:GetPlayers()) do, the otherPlayer would be v?

0

It's a PlayerObject, yes. Just make sure to check if 'v' isn't the player that fired the event.

This discussion is preserved as it was. New answers, comments, and votes are closed.