How to clone each time a player joins?

Hello! I’m currently having an issue for cloning a instance. What I want to achieve is that every time a player joins a part is created. Another joins, another part/instance is created. I’ve tried using things like PlayerAdded, although it just doesn’t work

Here is my code:

local cloned = game.ServerStorage.bright:Clone()
local repstor = game.ReplicatedStorage
local on = repstor.FlashOn
local off = repstor.FlashOff
local weld = Instance.new("WeldConstraint")
local charVariable

local bright = Instance.new("Part") -- The Instance
bright.Name = "bright"
bright.Transparency = 1
bright.Size = Vector3.new(0.125, 0.125, 0.125)
bright.CanCollide = true
bright.Anchored = false

local lighting = Instance.new("SpotLight")
lighting.Angle = 90
lighting.Brightness = 0
lighting.Shadows = true
lighting.Parent = bright

local brightclone = bright:Clone()
brightclone.Name = "brightclone"

game.Players.PlayerAdded:Connect(function() -- Every time a player joins
	brightclone.Parent = game.Workspace
end)


if brightclone then -- detects if there is a clone, then applies lighting
	game.Players.PlayerAdded:Connect(function(player)
		local backpack = player:FindFirstChildOfClass("Backpack")
		player.CharacterAdded:Wait()
		charVariable = player.Character
		weld.Parent = brightclone
		weld.Part0 = brightclone
		repstor.ForSecondLight.OnServerEvent:Connect(function()
			weld.Part1 = charVariable.flashlight.Handle
			brightclone.Position = charVariable.flashlight.lightpart.Position
			brightclone.Orientation = charVariable.flashlight.lightpart.Orientation
			brightclone.CanCollide = false
			wait()
		end)
	end)
	on.OnServerEvent:Connect(function() -- Other Lighting
		brightclone.SpotLight.Brightness = 5
	end)
	off.OnServerEvent:Connect(function()
		brightclone.SpotLight.Brightness = 0
	end)
end

(Every time I run this script on a multiplayer server it only clones once, not twice, etc.)

Place this inside PlayerAdded, as you are only telling the code to clone the Object once.

2 Likes

I have 2 functions that have player added inside of them, one with the clone.

game.Players.PlayerAdded:Connect(function() -- Every time a player joins
	brightclone.Parent = game.Workspace
end)

(this also only clones once every time many players join)

Hey there. A few things I wanted point out.

Why exactly are you using 2 PlayerAdded event listeners when they can be simply integrated into one?

Secondly, your if brightclone then condition seems redundant as it will never be false or nil. Based on your current code, it can be fully removed.

Thirdly, it isn’t a good idea to have your OnServerEvent function inside of a recurring event such as PlayerAdded as a new event connection gets made each time a player joins.

Lastly, for your cloning issue, xGOA7x’s response above should be a sufficient fix (disregarding the rest of the issues in your code).

A clear short answer for the question in the title

local RS = game:GetService("ReplicatedStorage")
local Plrs = game:GetService("Players")
local Item = RS:WaitForChild("whatever")
Plrs.PlayerAdded:Connect(function()
   local NewPart = Item:Clone()
   NewPart.Parent = workspace
end)
1 Like

Thanks! I’ll intergrade your suggestions into the script.

Also, for when I do this it doesn’t work.