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

trouble with player added event?

Asked by 9 years ago

I am trying to create a script that will remove the players limbs when they join, but when I tried to remove a limb I get the error message that says "tempNameHolder is not a valid part of workspace"

local players = game.Players
local tempNameHolder 

function onPlayerJoin(player)
    tempNameHolder = player.Name    
    game.Workspace.tempNameHolder.Left Leg:Destroy() -- removes left leg

end

players.PlayerAdded:connect(onPlayerJoin, player)

1 answer

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

First of all, connect only takes one parameter. player isn't defined anyway, so that wouldn't accomplish anything.


Now, the next problem is the way you are indexing children.

When you do Workspace.tempNameHolder, you ask for the object named "tempNameHolder" -- not the object name with the value that that variable holds.

To ask for the child with the name of that value, we can use the square braces to index:

game.Workspace[tempNameHolder]

You have a similar problem with "Leg Leg". Since "Left Leg" isn't a valid identifier, you can't index it using . (the space is the problem). Instead we use a string literal and the square braces again:

game.Workspace[tempNameHolder]["Left Leg"]:Destroy()

You can think of . in terms of [] (which is how Lua does it!):

-- These two are the same thing:
game.Workspace
-- and
game["Workspace"]
-- (using "Workspace" as a string)

One final problem -- your script will try to remove a leg the moment the player joins, even though their character won't be spawned yet.

We can add a wait of some kind to make sure they have a player, and that will work -- but remember it will only work on the first spawn.

You can use the player's CharacterAddedevent to delete it on all future spawns.

Ad

Answer this question