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

What is wrong with this simple script?

Asked by 10 years ago

Hi there! What is wrong with this simple script? Thanks in advance!

TeamColor = BrickColor.new("Bright blue") ---For this script to work, i need --the exact color of the team that may touch it only replace the stuff inside ""

script.Parent.Touched:connect(function(hit)
    print(hit)
if hit.Parent.TeamColor == BrickColor.new("Bright blue") then   
    hit.Parent.Humanoid.Health = 0
    else return
end
end)

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

The Character (the physical component of the Player) doesn't have a TeamColor property. It's just a Model.

In addition, you currently aren't doing a check to prevent errors involving the possible non-existence of hit.Parent or of hit.Parent.Humanoid.


We have to get the Player object corresponding to the Character and check its TeamColor.

script.Parent.Touched:connect( function(hit)
    if hit.Parent then
        -- Check to prevent errors
        local character = hit.Parent
        local player = game.Players:GetPlayerFromCharacter(character)
        if player and player.TeamColor == TeamColor then
            -- It actually is a Player (not some other object)
            -- and they are on the `TeamColor` colored team
            character.Humanoid.Health = 0
        end
    end
end )
0
Thank you - but it doesn't work :( jjwood1600 215 — 10y
0
*what* doesn't work? BlueTaslem 18071 — 10y
0
Don't worry :) I'll work it out! jjwood1600 215 — 10y
0
Well, actually - i'm not sure - i thought I'd solved it because it wanted a name for the function so i did function die(hit) but it still doesn't work :( jjwood1600 215 — 10y
View all comments (2 more)
0
Sorry, I only posted the function itself without the connection, but forgot to mention it. I edited it to include the connection correctly now BlueTaslem 18071 — 10y
0
Thanks! jjwood1600 215 — 10y
Ad

Answer this question