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

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

Asked by 6 years ago
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)
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. User#20279 0 — 6y
1
I got that part from the roblox developer website, so I think it is right. PaliKai13 92 — 6y
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. User#20279 0 — 6y
0
oh PaliKai13 92 — 6y
View all comments (2 more)
0
How do I fix that? PaliKai13 92 — 6y
0
see my answer Gey4Jesus69 2705 — 6y

1 answer

Log in to vote
3
Answered by 6 years ago
Edited 6 years ago

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!

1
Sweet, thanks, it works! PaliKai13 92 — 6y
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. User#20279 0 — 6y
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 Gey4Jesus69 2705 — 6y
1
Question, It works for red, but not for blue. It gives me red's outfit on both teams. PaliKai13 92 — 6y
1
print() works because it basically just uses the tostring() function ihatecars100 502 — 6y
Ad

Answer this question