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

How to change players clothes when an event is fired?

Asked by 3 years ago
local players = game:GetService("Players")
local plr = players.LocalPlayer
local plrteam = game:GetService("Teams").Red

game.ReplicatedStorage.Team.OnServerEvent:Connect(function()
    if game.Players.LocalPlayer.Character:FindFirstChild("Shirt") then
        game.Players.LocalPlayer.Character.Shirt:Destroy()
    end
    if game.Players.LocalPlayer.Character:FindFirstChild("Shirt Graphic") then
        game.Players.LocalPlayer.Character:FindFirstChild('Shirt Graphic'):Destroy()
    end
    if game.Players.LocalPlayer.Character:FindFirstChild("Pants") then
        game.Players.LocalPlayer.Character.Pants:Destroy()
    end

    local Shirt = Instance.new('Shirt',game.Players.LocalPlayer.Character)
    local Pant = Instance.new('Pants',game.Players.LocalPlayer.Character)

    Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=144076357"
        Pant.PantsTemplate = "http://www.roblox.com/asset/?id=144076759"
      end)

This is my script. The point of this is to change the players shirt and pants when an event is fired. When I run it , it tells me "Attempt to index nil "Character"" in the output. I spent 2 hrs fixing this script but now this is a problem I cant fix so it would be appreciated if you could help me. Thanks!

1 answer

Log in to vote
1
Answered by 3 years ago

I don't think u have to destroy anything ... like from line 06 to line 14 its just nonsense. just define the shirt and pant (Character.Shirt and .Pants) and change their Template as u did on line 19 and 20.

In Addition to that about the error, you have players.LocalPlayer so on a server script ... that doesn't work LocalPlayer is used in local scripts that's why its saying nil "Character" it can't find it.

To fix that you have to go to the script and add to :function() player as an argument so u should have

game.ReplicatedStorage.Team.OnServerEvent:Connect(function(player)

(If u asking why, well because :FireClient() on a local script will send the player automatically as the first parameter to the server script)

and u use that player to get the character

game.ReplicatedStorage.Team.OnServerEvent:Connect(function(player)
    local character = player.Character
0
Thanks so much , you let me learn something new. Thanks again and I have fixed it RebornedSnoop 175 — 3y
Ad

Answer this question