Hello! This is similar to something I've asked previously and I've edited it accordingly and tried out more things but it just refuses to work! Any ideas?
This code is supposed to build an item when someone joins the game, at certain coordinates (which works fine) but it is also supposed to remove it when the appropriate player leaves the game. I think the trouble is, I cannot call on the ships and I'm not sure how to name clones or refer to them later on in order to remove them.
function CreateShip(xcoord,ycoord,zcoord,AltName) 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 CreateShip(-1005,36,470,Ship01) workspace.Ship.Ship1Val.Value = true workspace.Ship.Owner1.Value = player.Name elseif workspace.Ship.Ship2Val.Value == false then CreateShip(-1005,36,450,Ship02) 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 ShipName = workspace.Ship:WaitForChild(Ship01) ShipName:remove() workspace.Ship.Owner1.Value = nil workspace.Ship.Ship1Val.Value = false elseif p.Name == workspace.Ship.Owner2.Value then local ShipName = workspace.Ship:WaitForChild(Ship02) ShipName:remove() workspace.Ship.Owner2.Value = nil workspace.Ship.Ship2Val.Value = false end end)
You have not used :WaitForChild
properly in this case. Also you would rather do game.Workspace
instead of workspace
Also if you haven't set AltName
to a string value
earlier in the script, it wont work. So if you do not have AltName
as a string value earlier in the script, then swap AltName
with "INSERTNAMEHERE" with the quotation marks, and change INSERNAMEHERE to your desired name
Here is what I think would work for you:
function CreateShip(xcoord,ycoord,zcoord,AltName) 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 game.Workspace.Ship.Ship1Val.Value == false then CreateShip(-1005,36,470,Ship01) game.Workspace.Ship.Ship1Val.Value = true game.Workspace.Ship.Owner1.Value = player.Name elseif game.Workspace.Ship.Ship2Val.Value == false then CreateShip(-1005,36,450,Ship02) game.Workspace.Ship.Ship2Val.Value = true game.Workspace.Ship.Owner2.Value = player.Name end end) game.Players.PlayerRemoving:connect(function(p) if p.Name == game.Workspace.Ship.Owner1.Value then local ShipName = workspace.Ship:WaitForChild("Ship01") ShipName:remove() game.Workspace.Ship.Owner1.Value = nil game.Workspace.Ship.Ship1Val.Value = false elseif p.Name == game.Workspace.Ship.Owner2.Value then local ShipName = workspace.Ship:WaitForChild("Ship02") ShipName:remove() game.Workspace.Ship.Owner2.Value = nil game.Workspace.Ship.Ship2Val.Value = false end end)
If this answer helped you out, make sure you accept the answer! :-)