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

How can I make a model disappear when someone leaves the game?

Asked by 8 years ago

This script is supposed to create a model when a player enters the game, create another in a second location when someone else joins and remove the appropriate model when they leave the game. I'm exhausted and my knowledge of this syntax is limited. Send help, the model's appear fine but when a play leaves the game neither of them will disappear


function CreateShip(xcoord,ycoord,zcoord) local NewShip = workspace.Ship:clone() NewShip.Parent = game.Workspace NewShip:makeJoints() NewShip:MoveTo(Vector3.new(xcoord,ycoord,zcoord)) NewShip.Name = AltName end game.Players.PlayerAdded:connect(function(player) if workspace.Ship.Ship1Val.Value == false then local AltName = player.Name CreateShip(-1005,36,470) workspace.Ship.Ship1Val.Value = true workspace.Ship.Owner1.Value = player.Name elseif workspace.Ship.Ship2Val.Value == false then local AltName = player.Name CreateShip(-1005,36,450) workspace.Ship.Ship2Val.Value = true workspace.Ship.Owner2.Value = player.Name end end) game.Players.PlayerRemoving:connect(function(p) if p.Name == workspace.Ship.Owner1.Value then local AltName = p.Name local ShipName = workspace.Ship:WaitForChild(AltName) ShipName:remove() workspace.Ship.Owner1.Value = nil workspace.Ship.Ship1Val.Value = false elseif p.Name == workspace.Ship.Owner2.Value then local AltName = p.Name local ShipName = workspace.Ship:WaitForChild(AltName) ShipName:remove() workspace.Ship.Owner2.Value = nil workspace.Ship.Ship2Val.Value = false end end)

I suspect it's an issue with naming the cloned models in the first place as I cannot refer to them easily, however I have no idea how to approach this problem and have tried multiple varying efforts to no avail Many Thanks, Sam

1 answer

Log in to vote
2
Answered by 8 years ago
Edited 8 years ago

I would set the ships name to the name of the player, or you could add a string value to the ship and set it to the player's name. when the player exit's the game you could check each ship for the player's name and destroy it if the name matches.

To do this a for loop should work.

game.Players.PlayerRemoving:connect(function(p)
   for _, ship in pairs(workspace:GetChildren()) do
          if p.Name == workspace.Ship.Owner1.Value then
            local AltName = p.Name
            local ShipName = workspace.Ship:WaitForChild(AltName)
            ShipName:remove()
            workspace.Ship.Owner1.Value = nil
            workspace.Ship.Ship1Val.Value = false
        elseif p.Name == workspace.Ship.Owner2.Value then
            local AltName = p.Name
            local ShipName = workspace.Ship:WaitForChild(AltName)
            ShipName:remove()
            workspace.Ship.Owner2.Value = nil
            workspace.Ship.Ship2Val.Value = false
        end
   end
end)

This is just an example in case you needed help with loops. You can find more information about loops here

1
I'm aware the example code doesn't work. The amount of changes I'd have to make to the script would have the end result as well. User#5748 20 — 8y
Ad

Answer this question