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):
game.Players.PlayerAdded:Connect(function(player) --My Possible problem local plr = game.Players:WaitForChild(player.Name) --My Possible problem local Seat = script.Parent local humanoid = nil local function sat() if humanoid == nil then humanoid = Seat.Occupant return end wait(3) game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent"):FireClient(plr)--My Possible problem humanoid = nil end Seat:GetPropertyChangedSignal("Occupant"):Connect(sat) end)
LocalScript (StarterPlayerScripts):
game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent").OnClientEvent:Connect(function() local Humanoid = game.Players.LocalPlayer.Character.Humanoid local ActiveTracks = Humanoid:GetPlayingAnimationTracks() for _,v in pairs(ActiveTracks) do v:Stop() end 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.
local hum = Seat.Occupant -- Gets the humanoid of the player local char = hum.Parent -- Get the character of the player local plr = game.Players:GetPlayerFromCharacter(char) -- Gets the player
And this is what it must look in your code :
game.Players.PlayerAdded:Connect(function() local Seat = script.Parent local humanoid = nil local function sat() if humanoid == nil then humanoid = Seat.Occupant return end local char = humanoid.Parent local plr = game.Players:GetPlayerFromCharacter(char) if plr then wait(3) game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent"):FireClient(plr) humanoid = nil end end Seat:GetPropertyChangedSignal("Occupant"):Connect(sat) end)
Lemme know if it helps!
Ok so Instead of:
game.Players.PlayerAdded:Connect(function() local Seat = script.Parent local humanoid = nil local function sat() if humanoid == nil then humanoid = Seat.Occupant return end local hum = Seat.Occupant local char = hum.Parent local plr = game.Players:GetPlayerFromCharacter(char) if plr then wait(3) game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent"):FireClient(plr) humanoid = nil end end Seat:GetPropertyChangedSignal("Occupant"):Connect(sat) end)
I just removed Line 09 because "humanoid" was already the "Seat.Occupant"
game.Players.PlayerAdded:Connect(function() local Seat = script.Parent local humanoid = nil local function sat() if humanoid == nil then humanoid = Seat.Occupant return end local char = humanoid.Parent --Changed to "Humanoid" instead of "hum" local plr = game.Players:GetPlayerFromCharacter(char) if plr then wait(3) game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent"):FireClient(plr) humanoid = nil end end Seat:GetPropertyChangedSignal("Occupant"):Connect(sat) end)
Many thanks to BestCreativeBoy for giving me most of the correct code. And TTChaos For helping too.