How to clone thing only once

So, here is my script for spawning a Dummies. I want to limit a number of Dummies to one for each player, how would I do that?

This script is placed in the ServerScriptService:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local spawnEvent = ReplicatedStorage:WaitForChild("SpawnEvent")
local dummy = ReplicatedStorage:WaitForChild("Dummy")

function SpawnDummy(player)
	local dummyClone = dummy:Clone()
	dummyClone.Parent = game.Workspace
	dummyClone.Name = player.Name.."'s "..dummy.Name
	dummyClone.Owner.Value = player.Name
end

spawnEvent.OnServerEvent:Connect(SpawnDummy)

Thanks!

You could attach a bool value to the player and set it as true when they spawn a dummy, after that you could use something similar to a debounce and not allow a player to spawn a dummy if the value is true.

Not entirely sure if this will work though.

I don’t understand what you mean. Do you want to spawn a dummy for every player in the server?

If so, then you would need to get all the players in the server. This could be useful for that: How to detect how many players in a server? - #6 by Shinjaa

I can try this after I try out one idea.

1 Like

I have gui button that spawns a dummy for a player that clicked it.

Try this

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local spawnEvent = ReplicatedStorage:WaitForChild("SpawnEvent")
local dummy = ReplicatedStorage:WaitForChild("Dummy")

local playerDummiesSpawned = {} 

function SpawnDummy(player)
if table.find(playerDummiesSpawned,player) then return end
table.insert(playerDummiesSpawned,player)
	local dummyClone = dummy:Clone()
	dummyClone.Parent = game.Workspace
	dummyClone.Name = player.Name.."'s "..dummy.Name
	dummyClone.Owner.Value = player.Name
end

spawnEvent.OnServerEvent:Connect(SpawnDummy)

It would still restrict spawning if the player leaves and rejoin. I’m not sure what your purpose is but this is a way to do it.

1 Like

aaand that seems to work! ill mark solution if i make sure that it works too after dummy dies

You’ll need to remove the player from the array using table.remove if you want it to work again after it dies.

Something like

Humanoid.Died:Connect(function() 
 table.remove(playerDummiesSpawned, table.find(playerDummiesSpawned,player)) 
end) 
1 Like

thats what i was thinking. lemme check real quick if it works.

edit: all good, thanks!

1 Like
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local spawnEvent = ReplicatedStorage:WaitForChild("SpawnEvent")
local dummy = ReplicatedStorage:WaitForChild("Dummy")
local ifCloned = false

function SpawnDummy(player)
	if ifCloned then
		return
	end
	local dummyClone = dummy:Clone()
	dummyClone.Parent = game.Workspace
	dummyClone.Name = player.Name.."'s "..dummy.Name
	dummyClone.Owner.Value = player.Name
	ifCloned = true
end

spawnEvent.OnServerEvent:Connect(SpawnDummy)

Just add a Boolean variable which represents if a clone has been made or not.

thats easier and probably less messy but, does it have any other benefits?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local spawnEvent = ReplicatedStorage:WaitForChild("SpawnEvent")
local dummy = ReplicatedStorage:WaitForChild("Dummy")

function SpawnDummy(player)
	local dummyClone = dummy:Clone()
	dummyClone.Parent = game.Workspace
	dummyClone.Name = player.Name.."'s "..dummy.Name
	dummyClone.Owner.Value = player.Name
	spawnEvent.OnServerEvent:Disconnect()
end

spawnEvent.OnServerEvent:Connect(SpawnDummy)

boolean seems better cause you can add cooldown there

Yeah, you can turn it into a debounce if you wish, like so:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local spawnEvent = ReplicatedStorage:WaitForChild("SpawnEvent")
local dummy = ReplicatedStorage:WaitForChild("Dummy")
local ifCloned = false

function SpawnDummy(player)
	if ifCloned then
		return
	end
	ifCloned = true
	local dummyClone = dummy:Clone()
	dummyClone.Parent = game.Workspace
	dummyClone.Name = player.Name.."'s "..dummy.Name
	dummyClone.Owner.Value = player.Name
	task.wait(5) --cooldown
	ifCloned = false
end

spawnEvent.OnServerEvent:Connect(SpawnDummy)

I just noticed “if ifCloned” looks weird, feel free to change it from that.

yeah thanks, that works really well too