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

How to work my on death function?

Asked by 10 years ago

I made a table with listed parts of a player. I'm trying to have my script go to the player who died, Anchor them then make them CanCollide false. I'll be adding more but this is currently not working as it is.

BodyParts = {Head,Torso,("Left Arm"),("Left Leg"),("Right Arm"),("Right Leg")}--6

game:GetService('Players').PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            for i,v in pairs(BodyParts) do
                player.v.Anchored = true
                player.v.CanCollide = false
            end
        end)
    end)
end)

What I'm planing to do is have the script go to my table and find each listed part, Change then change there properties when they die. But I keep getting the error V is not a valid member of Player Basically trying to make my own death effect.

0
A side question... Is there a way to reduce the time the body parts repsawn? alphawolvess 1784 — 10y

2 answers

Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

There are two problems here.

On line 1, you set the first two entries to 'nil', because Head and Torso are not defined; you have to mark them as strings. Additionally, the parentheses around the rest of the strings are not required.

Inside your generic for loop, Lua thinks you're trying to access the property or child of player with the name "v", not the name from your Table. To get around this, you have to use the bracket access operator. Also, the Character contains the body parts, not the Player.

Your fixed code should look like this:

BodyParts = {"Head","Torso",("Left Arm"),("Left Leg"),("Right Arm"),("Right Leg")}--6

game:GetService('Players').PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            for i,v in pairs(BodyParts) do
                character[v].Anchored = true
        --This is the bracket access operator. Notice the lack of the first period (.) operator. You use this whenever children have spaces in their names, or when you're using a variable of some kind, as you are attempting to do here.
                character[v].CanCollide = false
            end
        end)
    end)
end)

EDIT:

Making a custom respawn time is simple, actually. This has to be done in a Script, however, not a LocalScript.

First, you set the CharacterAutoLoads property of Players to false. Next, you call the LoadCharacter method on the Player you wish to respawn, after waiting a suitable amount of time. The default for this is about 5 seconds, so this code will approximate ROBLOX's standard respawn time:

game.Players.CharacterAutoLoads = false

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            wait(5) --This determines the time between respawns.
            player:LoadCharacter()
        end)
    end)

    player:LoadCharacter() --loads the Character for the first time.
end)
0
What I did for Left Leg and so on works too? i.e. ("Left Leg") I could just do ["Left Leg"]? alphawolvess 1784 — 10y
1
No, that won't work. Either drop the parentheses or keep them, but don't change them to square brackets. Square brackets inside of a Table declaration tell the script that you're setting a string index, rather than the default numeric one. adark 5487 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

Make sure you also add the Head and Torso in quotes. The body parts are located inside the character not the player. Also add brackets around the v

BodyParts = {"Head" ,"Torso" ,"Left Arm","Left Leg","Right Arm","Right Leg"}

game:GetService('Players').PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            for i,v in pairs(BodyParts) do
               character[v].Anchored = true
              character[v].CanCollide = false
            end
        end)
    end)
end)

Answer this question