Cloning Model is Replacing Another Player's Model [FIXED]

Currently I am scripting a spawning system for my game where the players will take clones of a replicated storage model depending on which button they press. This model will then be renamed to the players name with spacecraft on the end. When they choose the same one however it gets weird. I even have an “Owner” attribute which gets set to the players name to determine that it is theirs. However when I am running this code it somehow overlooks this and totally replaces the other players model that was spawned in and then both their CameraSubjects are stuck on the same model. I’m not sure why this is happening, I have been doing lots of research and have spent many hours trying to figure this out, but don’t understand why it does this. How could I fix this or what am I missing?

Video Reference

Explorer

Local Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SelectionGUI = script.Parent.Frame

local ExitGUI = script.Parent.Parent:WaitForChild("ExitButton").Frame

local CuriosityButton = script.Parent.Frame.Curiosity
local SpiritButton = script.Parent.Frame.Spirit

local SpacecraftSelected = ReplicatedStorage.SpacecraftSelected
local SelectionVerified = ReplicatedStorage.SelectionVerified

local selection = ReplicatedStorage.Values.Selection

local plr = game.Players.LocalPlayer

--[[ 
Selection Values

Not selected yet: 0
Spirit: 1
Opportunity: 2 --- Not being used yet
Curiosity: 3

]]--

SpiritButton.MouseButton1Click:Connect(function(plr)
	if selection.Value == 0 then
		selection.Value = 1
		wait()
		SpacecraftSelected:FireServer(selection.Value)
	else
		print("You already have a spacecraft spawned!")
	end
	
end)

CuriosityButton.MouseButton1Click:Connect(function(plr)
	if selection.Value == 0 then
		selection.Value = 3
		wait()
		SpacecraftSelected:FireServer(selection.Value)
	else
		print("You already have a spacecraft spawned!")
	end
end)

--Verified--
SelectionVerified.OnClientEvent:Connect(function()
	local children = game.Workspace:GetChildren()
	print(plr)
	for i = 1, #children do
		local Child = children[i]:GetAttribute("Owner")
		if Child == plr.Name then
			SelectionGUI.Visible = false
			ExitGUI.Visible = true
			workspace.CurrentCamera.CameraSubject = children[i].SpacecraftMain
		elseif Child == nil then
			print("Not yet")
		else
			print("Error")
		end
	end
end)

Script

--Services--
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--Events--
local SpacecraftSelected = ReplicatedStorage.SpacecraftSelected
local SelectionVerified = ReplicatedStorage.SelectionVerified

--Spacecrafts--
local CuriosityRover = ReplicatedStorage.Curiosity
local SpiritRover = ReplicatedStorage.Spirit
local CuriosityClone = ReplicatedStorage.Curiosity:Clone()
local SpiritClone = ReplicatedStorage.Spirit:Clone()

local WorkspaceChildren = game.Workspace:GetChildren()

--Main Function
SpacecraftSelected.OnServerEvent:Connect(function(plr, selection)
	print(selection)
	for i = 1, #WorkspaceChildren do
		local Child = WorkspaceChildren[i]:GetAttribute("Owner")
		if Child == plr.Name then
			print("This player has already spawned a spacecraft!")
			break
		else
			print("This player has not spawned a spacecraft yet!")
		end
	end
	if selection == 0 then
		print("Error")
	elseif selection == 1 then
		SpiritClone.Parent = game.Workspace
		SpiritClone.Name = plr.Name..("Spacecraft")
		SpiritClone:SetAttribute("Owner", plr.Name)
		SelectionVerified:FireClient(plr)
	elseif selection == 2 then
		print("Not avalaiable yet!")
	elseif selection == 3 then
		CuriosityClone.Parent = game.Workspace
		CuriosityClone.Name = plr.Name..("Spacecraft")
		CuriosityClone:SetAttribute("Owner", plr.Name)
		SelectionVerified:FireClient(plr)
	else
		print("Something else is wrong")
	end
end)

Any help is appreciated, thanks!

Shortened in my way.

Client

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SelectionGUI = script.Parent.Frame

local ExitGUI = script.Parent.Parent:WaitForChild("ExitButton").Frame
--What's this

local CuriosityButton = SelectionGUI.Curiosity
local SpiritButton = SelectionGUI.Spirit

local SpacecraftSelected = ReplicatedStorage.SpacecraftSelected
local SelectionVerified = ReplicatedStorage.SelectionVerified

local selection = ReplicatedStorage.Values.Selection

--[[ 
Selection Values

Not selected yet: 0
Spirit: 1
Opportunity: 2 --- Not being used yet
Curiosity: 3

]]--

SpiritButton.MouseButton1Click:Connect(function(plr)
--plr meant to be LocalPlayer
	if selection.Value == 0 then
		selection.Value = 1
		wait()
		SpacecraftSelected:FireServer(selection.Value)
	else
		print("You already have a spacecraft spawned!")
	end
	
end)

CuriosityButton.MouseButton1Click:Connect(function(plr)
--Same
	if selection.Value == 0 then
		selection.Value = 3
		wait()
		SpacecraftSelected:FireServer(selection.Value)
	else
		print("You already have a spacecraft spawned!")
	end
end)

--Verified--
SelectionVerified.OnClientEvent:Connect(function()
	local plr = game.Players.LocalPlayer
	local children = workspace:GetChildren()
	print(plr)
	for i = 1, #children do
		local Child = children[i]:GetAttribute("Owner")
		if Child then
			if Child == plr.Name then
			SelectionGUI.Visible = false
			ExitGUI.Visible = true
			workspace.CurrentCamera.CameraSubject = children[i].SpacecraftMain
			else
			print("Error")
			end
		else--Child is nil and comes to here
			print("Not yet")
		end
	end
end)

Server

--Services--
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--Events--
local SpacecraftSelected = ReplicatedStorage.SpacecraftSelected
local SelectionVerified = ReplicatedStorage.SelectionVerified

--Spacecrafts--
local CuriosityRover = ReplicatedStorage.Curiosity
local SpiritRover = ReplicatedStorage.Spirit
local CuriosityClone = CuriosityRover:Clone()
local SpiritClone = SpiritRover:Clone()
--Variables are pointing same object.

local WorkspaceChildren = workspace:GetChildren()

--Main Function
SpacecraftSelected.OnServerEvent:Connect(function(plr, selection)
	print(selection)
	for i = 1, #WorkspaceChildren do
		local Child = WorkspaceChildren[i]:GetAttribute("Owner")
		if Child == plr.Name then
			print("This player has already spawned a spacecraft!")
			break
		else
			print("This player has not spawned a spacecraft yet!")
		end
	end
	if selection == 0 then
		print("Error")
	elseif selection == 1 then
		SpiritClone.Parent = workspace
		SpiritClone.Name = plr.Name..("Spacecraft")
		SpiritClone:SetAttribute("Owner", plr.Name)
		SelectionVerified:FireClient(plr)
	elseif selection == 2 then
		print("Not avalaiable yet!")
	elseif selection == 3 then
		CuriosityClone.Parent = workspace
		CuriosityClone.Name = plr.Name..("Spacecraft")
		CuriosityClone:SetAttribute("Owner", plr.Name)
		SelectionVerified:FireClient(plr)
	else
		print("Something else is wrong")
	end
end)

Did you change something or what do you mean?

I figured out the issue in my code. So for anyone in the future looking for how to do this. The reason it was not working for me was because I was using the same cloned part continuously so when the player selected the same model it would just replace it, but to make spawn new ones for each player I had to clone it inside of the for loop rather than setting the clone as variable at the top

2 Likes

Check comment in script. And congrats for solving your issue.

1 Like

Thanks I appreciate the help. Also I found the comment in the script.