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

My argument doesn't works? (player.Name) Please help

Asked by 4 years ago
Edited 4 years ago

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):

01game.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)
16end)

LocalScript (StarterPlayerScripts):

1game: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
7end)

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.

0
I would like you edit your :FireClient(plr) to :FireClient(player, plr) and in Local Script add this line in line 2, if plr == game.Players.LocalPlayer.Name then and your code BestCreativeBoy 1395 — 4y
0
Also add plr parameter in OnClientEvent BestCreativeBoy 1395 — 4y
0
@BestCreativeBoy well i did everything you told me, i tried the game but now nothing happens, animations are not removed. (There is no error in the output) benjinanas 50 — 4y
0
And I think I shouldn't edit the LocalScript because I don't think the problem Is in the LocalScript benjinanas 50 — 4y
View all comments (2 more)
0
hello? benjinanas 50 — 4y
0
Check the code again, I made an error. Now, it should work. BestCreativeBoy 1395 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

1local hum = Seat.Occupant -- Gets the humanoid of the player
2local char = hum.Parent -- Get the character of the player
3local plr = game.Players:GetPlayerFromCharacter(char) -- Gets the player

And this is what it must look in your code :

01game.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)
20end)

Lemme know if it helps!

0
The output says: FireClient: player argument must be a Player object benjinanas 50 — 4y
0
Put "if plr then" before line 12 and put "end" on line 15 User#30567 0 — 4y
0
Put "if plr then" before line 12 and put "end" on line 15 User#30567 0 — 4y
0
It also didn't work but there was no error in the output benjinanas 50 — 4y
View all comments (7 more)
0
(btw, It didn't remove any animation) benjinanas 50 — 4y
0
If the plr value is nil, the further code won't execute. Also, there is one more way, i.e., by using Touched event and getting the player from there. Try using that BestCreativeBoy 1395 — 4y
0
I am sorry all, I made a follish error. Check the script again. It might work now BestCreativeBoy 1395 — 4y
0
*foolish BestCreativeBoy 1395 — 4y
0
Script:10: attempt to index nil with 'Parent'. I guess Parent doesn't works with Occupant benjinanas 50 — 4y
0
Oh thank you! I mean it didn't worked but I just removed Line 09 and replaced "hum" By "humanoid" Because of the Line 06 benjinanas 50 — 4y
0
Edited the script for other with similar issue BestCreativeBoy 1395 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Ok so Instead of:

01game.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)
View all 21 lines...

I just removed Line 09 because "humanoid" was already the "Seat.Occupant"

01game.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)
20end)

Many thanks to BestCreativeBoy for giving me most of the correct code. And TTChaos For helping too.

Answer this question