Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't my code work? won't get past if player then.

function onTouched(part)
    local character = part.Parent
    local players = game:GetService("Players")
    local player = players:GetPlayerFromCharacter(part.Parent)
    local shirt = character:FindFirstChildOfClass("Shirt")
    local pants = character:FindFirstChildOfClass("Pants")
    if player then
        if not shirt then 
                shirt = Instance.new("Shirt", character)
        end 
        if not pants then 
                pants = Instance.new("Pants", character)
        end
        if player.Team == "Red" then
            shirt.ShirtTemplate = "rbxassetid://136056214"
            pants.PantsTemplate = "rbxassetid://136056214"
        end
        if player.Team == "Blue" then
            shirt.ShirtTemplate = "rbxassetid://136056667"
            pants.PantsTemplate = "rbxassetid://136056667"
        end
    end
end

script.Parent.Touched:Connect(onTouched)

6 comments

0

Not sure if I’m correct, but I believe it’s the “not”s in your conditional statement (if-then) that’s causing the problem. I think “not nil” wouldn’t be “true”(make the conditional statement go through), so instead put “shirt == nil” and “pants == nil” and see if it works.

1

I got that part from the roblox developer website, so I think it is right.

0

^ I see, I’ll just go double check to see if I’m wrong then and find what the problem is. Also note this script is supposed to work only when the player doesn’t have a shirt or pants.

0

oh

0

How do I fix that?

0

see my answer

1 answer

✓ Accepted answer

The Team property returns an object value (the Team in game.Teams). In lines 14-21 you should be doing:

        if player.Team.Name == "Red" then
            shirt.ShirtTemplate = "rbxassetid://136056214"
            pants.PantsTemplate = "rbxassetid://136056214"
        end
        if player.Team.Name == "Blue" then
            shirt.ShirtTemplate = "rbxassetid://136056667"
            pants.PantsTemplate = "rbxassetid://136056667"
        end

Therefore, your script was passing the check on the player, but it wasn't passing either of these, as you were checking if an object value was equal to a string value.

Resources:

Team

Accept and upvote if this helps!

5 comments

1

Sweet, thanks, it works!

1

Ah shoot, I didn't realize that. I just assumed referencing the team object itself would use its name like when using print(). Oh well.

1

that only works with print() because of some reason i cant remember, hehe. but ur right, it does work that way with the print function

1

Question, It works for red, but not for blue. It gives me red's outfit on both teams.

1

print() works because it basically just uses the tostring() function

This discussion is preserved as it was. New answers, comments, and votes are closed.