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

How can I make a script which will add an object when a player joins and remove it when they leave?

Asked by 4 years ago
Edited 4 years ago

I am making a game with my brother. I want to create a script which will create a row of cylinder pedestals according to how many people are currently in the game. I also want it to destroy one of the pedestals when a player leaves, so that there is always an equal amount of pedestals to players in the game.

I have inserted my current code below. This version of the script does not create any objects when i run the game. Before this version of the script, I had a simplified version with a for loop that executed the createPedestal() function and shifted the position each time, creating a row of pedestals.

some things to note as well, my createPedestal() function renames each part when it is created so i can tell them apart (e.g. pedestal1, pedestal2, etc.)

P.S. the code is in a script which is in workspace.

function createPedestal(position, num)
    local pedestal = Instance.new("Part")
    pedestal.Position = position
    pedestal.Name = "Pedestal"..num 
    pedestal.Orientation = Vector3.new(0,0,-90)
    pedestal.Size = Vector3.new(3,6,6)
    pedestal.Anchored = true
    pedestal.Parent = game.Workspace
    pedestal.Shape = Enum.PartType.Cylinder
    pedestal.Color = Color3.fromRGB(255,0,0)
    pedestal.Material = Enum.Material.Neon
end

function destroyPedestal(num)
    local temp = game.Workspace.pedestal
    temp:Destroy()
end

function onPlayerAdded(player)
    local num = script.Parent:FindFirstChild("Players") 
    num.Value = num.Value + 1
    createPedestal(Vector3.new(-200,-42.296, 135-(10*num.Value)), num.Value)
end

function onPlayerRemoved(player)
    local num = script.Parent:FindFirstChild("Players")
    num.Value = num.Value - 1
    destroyPedestal(num)
end

game.Players.ChildAdded:Connect(onPlayerAdded)
game.Players.ChildRemoved:Connect(onPlayerRemoved)

Update: I have gotten the pedestals to be added each time a player joins (thanks to greatneil80), now how can I remove a pedestal if one player leaves? the new code is below

local num = 0

function createPedestal(position, partNum)
    local pedestal = Instance.new("Part")
    pedestal.Position = position
    pedestal.Name = "Pedestal"..partNum 
    pedestal.Orientation = Vector3.new(0,0,-90)
    pedestal.Size = Vector3.new(3,6,6)
    pedestal.Anchored = true
    pedestal.Parent = game.Workspace
    pedestal.Shape = Enum.PartType.Cylinder
    pedestal.Color = Color3.fromRGB(255,0,0)
    pedestal.Material = Enum.Material.Neon
end

function destroyPedestal(num)
    local temp = game.Workspace.pedestal..num
    temp:Destroy()
end 

function onPlayerAdded(player)
    num = num + 1
    createPedestal(Vector3.new(-200,-42.296, 135-(10*num)), num)
end

function onPlayerRemoved(player)
    num = num - 1
    destroyPedestal(num)
end

game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoved)

I am quite new to Lua and I only know basics so far, so I would appreciate it for any explanations to be explained in depth, as well as explaining how the code works and why it runs like it does. (please also explain keywords and syntax if needed)

TL:DR -- To summarize, I want to create a script that adds a pedestal each time a player joins the game, and remove a pedestal each time a player leaves the game.

0
PlayerAdded PlayerRemoving greatneil80 2647 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Try this:

game.Players.PlayerAdded:Connect(function(p)
    local pedestal = Instance.new("Part")
    pedestal.Position = position
    pedestal.Name = "Pedestal_"..tostring(p.Name) -- if the players name is just numbers it will convert it into a string.
    pedestal.Orientation = Vector3.new(0,0,-90)
    pedestal.Size = Vector3.new(3,6,6)
    pedestal.Anchored = true
    pedestal.Parent = game.Workspace
    pedestal.Shape = Enum.PartType.Cylinder
    pedestal.Color = Color3.fromRGB(255,0,0)
    pedestal.Material = Enum.Material.Neon
    -- ugh i hate this kind of indent.
end)

game.Players.PlayerRemoving:Connect(function(p)
    workspace:FindFirstChild("Pedestal_"..tostring(p.Name)):Destroy()
end)

What you did was almost perfect, I just did a few changes, also I love the neatness in your code!

0
haha the neatness in my code comes from my experience of coding in java, thanks! Object oriented programming is the way to go! alivemaeonman 14 — 4y
0
Thanks for your help, the script works almost perfectly, but if there is one player left, the last pedestal does not delete, what should i do? alivemaeonman 14 — 4y
0
Nevermind, I just had to switch the 2 lines of code around in my onPlayerRemoved() function, thanks again! alivemaeonman 14 — 4y
0
Remember to click accept if this worked! greatneil80 2647 — 4y
Ad

Answer this question