I've been working on an RPG map and I'm stuck... What my aim here is when a player joins, they spawn right above the brick (The onTouch one), and when they hit it the script happens so I can have a custom GUI with a character selection etc. What I'm after is how can I make this more efficient and so it happens without the player touching the brick?
function onTouch(hit) if hit.Name == "Left Leg" then for _,v in pairs(hit.Parent:GetChildren()) do if v:IsA("CharacterMesh") then v:Remove() end if v:IsA("Shirt") then v:Remove() end if v:IsA("T-Shirt") then v:Remove() end if v:IsA("Pants") then v:Remove() end if v:IsA("Hat") then v:Remove() end local headMesh = game.ServerStorage.HeadMesh if headMesh ~= nil then local head = hit.Parent.Head local headMeshClone = headMesh:Clone() headMeshClone.Parent = head end end end end script.Parent.Touched:connect(onTouch)
Thanks, jakei181
You can do things when the character spawns by using the CharacterAppearanceLoaded
event, which is just like CharacterAdded, but it waits for the assets to load.
game.Players.PlayerAdded:connect(function(player) player.CharacterAppearanceLoaded:connect(function(character) for _,v in pairs(character:GetChildren())do if v:IsA("CharacterAppearance")then v:Destroy() end game.ServerStorage.HeadMesh:clone().Parent=character.Head end end) end)
An even better way to not have to deal with character assets is to set Player.CanLoadCharacterAppearance
to false when they join.
edit:
Player.CharacterAdded
is an event of the Player class. It is fired when a player respawns or when Player:LoadCharacter()
is called, and it provides the new character as an argument.
game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(functon(char) print(char.Name.."'s character spawned") end) end)
Player.CharacterAppearanceLoaded
does the same thing, but it doesn't fire immediately when the character spawns. Instead, it waits for the hats, packages, etc. to load into the character object. If you were to try to access (or remove) these objects before they loaded, they would not be there yet.