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.
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)
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)