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 3 years ago
Edited 3 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):

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.

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 — 3y
0
Also add plr parameter in OnClientEvent BestCreativeBoy 1395 — 3y
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 — 3y
0
And I think I shouldn't edit the LocalScript because I don't think the problem Is in the LocalScript benjinanas 50 — 3y
View all comments (2 more)
0
hello? benjinanas 50 — 3y
0
Check the code again, I made an error. Now, it should work. BestCreativeBoy 1395 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 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.

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!

0
The output says: FireClient: player argument must be a Player object benjinanas 50 — 3y
0
Put "if plr then" before line 12 and put "end" on line 15 User#30567 0 — 3y
0
Put "if plr then" before line 12 and put "end" on line 15 User#30567 0 — 3y
0
It also didn't work but there was no error in the output benjinanas 50 — 3y
View all comments (7 more)
0
(btw, It didn't remove any animation) benjinanas 50 — 3y
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 — 3y
0
I am sorry all, I made a follish error. Check the script again. It might work now BestCreativeBoy 1395 — 3y
0
*foolish BestCreativeBoy 1395 — 3y
0
Script:10: attempt to index nil with 'Parent'. I guess Parent doesn't works with Occupant benjinanas 50 — 3y
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 — 3y
0
Edited the script for other with similar issue BestCreativeBoy 1395 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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.

Answer this question