First of all, I am not an English speaker, so... sorry if my English is difficult to understand :(
Well... Im trying to make a script that execute a LocalScript (Via RemoteEvent), The LocalScript works to remove every animation that the player has and it works BUT here is my problem:
ServerScript, inside of the seat. (Execute the LocalScript when the player Jumps out of the seat):
01 | game.Players.PlayerAdded:Connect( function (player) --My Possible problem |
02 | local plr = game.Players:WaitForChild(player.Name) --My Possible problem |
03 | local Seat = script.Parent |
04 | local humanoid = nil |
05 | local function sat() |
06 | if humanoid = = nil then |
07 | humanoid = Seat.Occupant |
08 | return |
09 | end |
10 | wait( 3 ) |
11 | game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent" ):FireClient(plr) --My Possible problem |
12 | humanoid = nil |
13 | end |
14 |
15 | Seat:GetPropertyChangedSignal( "Occupant" ):Connect(sat) |
16 | end ) |
LocalScript (StarterPlayerScripts):
1 | game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent" ).OnClientEvent:Connect( function () |
2 | local Humanoid = game.Players.LocalPlayer.Character.Humanoid |
3 | local ActiveTracks = Humanoid:GetPlayingAnimationTracks() |
4 | for _,v in pairs (ActiveTracks) do |
5 | v:Stop() |
6 | end |
7 | end ) |
So, when I try the game, It works, The LocalScript Stop all the animations that the player has when the player jumps out of the seat BUT when I play in a LocalServer (2 Players) I noticed that when some player jump out of the seat, (I guess the ServerScript) stops ALL PLAYERS animations instead of the animations of the player who was sitting on that seat.
I looked in the Dev Hub and some guy said that if we put: (player.Name) It will execute the script just for that specific Client who sat in the seat. (Im not sure about it because its doesn't works for me). Thats all.
Please somebody answer me, If you need more details, tell me.
I think I might have found the error. You are accessing the player parameter from the PlayerAdded
Event. And, that you should not do, as it will fire everyone on the server.
In order to prevent this, you can get the Player
, who is occupying the seat.
1 | local hum = Seat.Occupant -- Gets the humanoid of the player |
2 | local char = hum.Parent -- Get the character of the player |
3 | local plr = game.Players:GetPlayerFromCharacter(char) -- Gets the player |
And this is what it must look in your code :
01 | game.Players.PlayerAdded:Connect( function () |
02 | local Seat = script.Parent |
03 | local humanoid = nil |
04 | local function sat() |
05 | if humanoid = = nil then |
06 | humanoid = Seat.Occupant |
07 | return |
08 | end |
09 | local char = humanoid.Parent |
10 | local plr = game.Players:GetPlayerFromCharacter(char) |
11 |
12 | if plr then |
13 | wait( 3 ) |
14 | game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent" ):FireClient(plr) |
15 | humanoid = nil |
16 | end |
17 | end |
18 |
19 | Seat:GetPropertyChangedSignal( "Occupant" ):Connect(sat) |
20 | end ) |
Lemme know if it helps!
Ok so Instead of:
01 | game.Players.PlayerAdded:Connect( function () |
02 | local Seat = script.Parent |
03 | local humanoid = nil |
04 | local function sat() |
05 | if humanoid = = nil then |
06 | humanoid = Seat.Occupant |
07 | return |
08 | end |
09 | local hum = Seat.Occupant |
10 | local char = hum.Parent |
11 | local plr = game.Players:GetPlayerFromCharacter(char) |
12 |
13 | if plr then |
14 | wait( 3 ) |
15 | game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent" ):FireClient(plr) |
I just removed Line 09 because "humanoid" was already the "Seat.Occupant"
01 | game.Players.PlayerAdded:Connect( function () |
02 | local Seat = script.Parent |
03 | local humanoid = nil |
04 | local function sat() |
05 | if humanoid = = nil then |
06 | humanoid = Seat.Occupant |
07 | return |
08 | end |
09 | local char = humanoid.Parent --Changed to "Humanoid" instead of "hum" |
10 | local plr = game.Players:GetPlayerFromCharacter(char) |
11 |
12 | if plr then |
13 | wait( 3 ) |
14 | game:GetService( "ReplicatedStorage" ):WaitForChild( "RemoteEvent" ):FireClient(plr) |
15 | humanoid = nil |
16 | end |
17 | end |
18 |
19 | Seat:GetPropertyChangedSignal( "Occupant" ):Connect(sat) |
20 | end ) |
Many thanks to BestCreativeBoy for giving me most of the correct code. And TTChaos For helping too.