Spawning a part from replicated storage

Hello, i have a model named part in replicated storage, I have two problems, when I spawn the clone, it does not spawn at where I am facing, second question is that my limit of only spawning one part does not work meaning I can spawn infinite clones
script(local script inside startercharacter)

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local camera = workspace.CurrentCamera
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local CurrentParts = 0

UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted and CurrentParts >= 1 then
		return
	end

	if Key.KeyCode == Enum.KeyCode.E then
		CurrentParts += 1
		Event:FireServer()
	end
end)

server script:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

Event.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	local HRP = Character:WaitForChild("HumanoidRootPart")

	local ClonedPart = game.ReplicatedStorage.Part:Clone()
	ClonedPart.Parent = workspace
	ClonedPart:MakeJoints() --Still don't understand why this is deprecated
	ClonedPart.PrimaryPart = ClonedPart.Torso --This is called first before calling the PrimaryPartCFrame function
	ClonedPart:SetPrimaryPartCFrame(CFrame.new(HRP.CFrame.LookVector * 5))
	ClonedPart:SetPrimaryPartCFrame(CFrame.new(HRP.CFrame.Position * 2))
end)

Is the reason the limit does not work, it’s checking if the game processed the event AND if the parts is greater than or equal to 1, the and should be an or

Think this can be done as so

ClonedPart:SetPrimaryPartCFrame(CFrame.new(HRP.CFrame.Position + (HRP.CFrame.LookVector * 5)))

uhhhhh i can spawn another one once i reset ;-;

It’s because the script is in StarterCharacterScripts, the script is remade once you respawn, put it somewhere else, such as StarterPlayerScripts

how would i make it so that it -1 if the server detects that the have lost one in the void or something like that

You’ll need to probably name the part to the name of the Player and listen for when the workspace loses a child via ChildRemoved or AncestryChanged on the clonedPart

For the subtraction, you’ll probably need to have a value somewhere t ohandle if the player had already spawned a part and reference that instead

i did this:

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Pp = game.ReplicatedStorage:WaitForChild("Pp")
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local CurrentParts = 0

UIS.InputBegan:Connect(function(Key, Chatted)
	if Chatted and CurrentParts >= 1 then
		return
	end

	if Key.KeyCode == Enum.KeyCode.E then
		CurrentParts += 1
		Event:FireServer()
	end
	if Key.KeyCode == Enum.KeyCode.Q and CurrentParts = 1 then
		CurrentParts -=1
		Pp:FireServer()
	end
end)

i named the delete clone event Pp (yes im lazy)
server script:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Pp = game.ReplicatedStorage:WaitForChild("Pp")
Event.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	local HRP = Character:WaitForChild("HumanoidRootPart")

	local ClonedPart = game.ReplicatedStorage.Part:Clone()
	ClonedPart.Parent = workspace
	ClonedPart:MakeJoints() --Still don't understand why this is deprecated
	ClonedPart.PrimaryPart = ClonedPart.Torso --This is called first before calling the PrimaryPartCFrame function
	ClonedPart:SetPrimaryPartCFrame(CFrame.new(HRP.CFrame.Position + (HRP.CFrame.LookVector * 5)))
end)
Pp.OnServerEvent:Connect(function(Player)
	
end)

i did not finish bc idk how to delete the part

You can probably store the part in a dictionary and get it from there and destroy it, something like this

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Pp = game.ReplicatedStorage:WaitForChild("Pp")

local createdParts = {}

Event.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	local HRP = Character:WaitForChild("HumanoidRootPart")

	local ClonedPart = game.ReplicatedStorage.Part:Clone()
	ClonedPart.Parent = workspace
	ClonedPart:MakeJoints() --Still don't understand why this is deprecated
	ClonedPart.PrimaryPart = ClonedPart.Torso --This is called first before calling the PrimaryPartCFrame function
	ClonedPart:SetPrimaryPartCFrame(CFrame.new(HRP.CFrame.Position + (HRP.CFrame.LookVector * 5)))
	createdParts[Player.Name] = ClonedPart
end)
Pp.OnServerEvent:Connect(function(Player)
	local part = createdParts[Player.Name]
	if not part then 
		return
	end
	part:Destroy()
end)

Although this can cause a problem since it doesn’t delete for players who left, so a better way is instead of parenting it to the workspace, parent it to a folder where the clones wil lbe kept, and naame the part the name of the Player who wanted it cloned, then in your destroy event, get the part via Folder:FindFirstChild(Player.Name) and check if it found something, and if it did, destroy the part

1 Like

how would i do it like that?
+
where would I put the ChildRemoved or AncestryChanged

I don’t think you’d need those events anymore since this would be a better approach,

Let’s asusme you have a Folder in workspace called Folder for these clones, you’d just do

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Pp = game.ReplicatedStorage:WaitForChild("Pp")

Event.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	local HRP = Character:WaitForChild("HumanoidRootPart")

	local ClonedPart = game.ReplicatedStorage.Part:Clone()
	ClonedPart.Parent = workspace.Folder
	ClonedPart.Name = Player.Name
	ClonedPart:MakeJoints() --Still don't understand why this is deprecated
	ClonedPart.PrimaryPart = ClonedPart.Torso --This is called first before calling the PrimaryPartCFrame function
	ClonedPart:SetPrimaryPartCFrame(CFrame.new(HRP.CFrame.Position + (HRP.CFrame.LookVector * 5)))
end)
Pp.OnServerEvent:Connect(function(Player)
	local part = workspace.Folder:FindFirstChild(Player.Name)
	if not part then 
		return
	end
	part:Destroy()
end)

how do i change it so that it says player’s name + 's clone
ex: foxnoobkite’s clone
(idk if roblox will allow space tho)

They do allow spaces in names, but it would be better to use underscores for spaces

ClonedPart.Name = Player.name.."'s_clone"

And in the destroy, you have to change it to

local part = workspace.Folder:FindFirstChild(Player.Name.."'s_clone")
  • the delete event don’t work now…[quote=“EmbatTheHybrid, post:10, topic:1203065”]
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Pp = game.ReplicatedStorage:WaitForChild("Pp")

Event.OnServerEvent:Connect(function(Player)
	local Character = Player.Character
	local HRP = Character:WaitForChild("HumanoidRootPart")

	local ClonedPart = game.ReplicatedStorage.Part:Clone()
	ClonedPart.Parent = workspace.Folder
	ClonedPart.Name = Player.Name
	ClonedPart:MakeJoints() --Still don't understand why this is deprecated
	ClonedPart.PrimaryPart = ClonedPart.Torso --This is called first before calling the PrimaryPartCFrame function
	ClonedPart:SetPrimaryPartCFrame(CFrame.new(HRP.CFrame.Position + (HRP.CFrame.LookVector * 5)))
end)
Pp.OnServerEvent:Connect(function(Player)
	local part = workspace.Folder:FindFirstChild(Player.Name)
	if not part then 
		return
	end
	part:Destroy()
end)

[/quote]

the delete event don’t work with this

Are you getting any errors? Is there a Folder in the workspace called Folder?

And what is your UserInputService script right now?

yes no errors and a folder
idk where is userinputservice is

The client script that handles firing the RemoteEvents, maybe it could be something wrong there? Although I think you may have to add print statements in both the localscript and the regular script

well there is this error:

 NameClonedPart is not a valid member of Player "Players.foxnoobkite"

Did you forget to put a space inbetween Name and ClonedPart

I don’t know what you’re trying, are you trying to clone a part and then add it to the player’s character…?

ClonedPart.Name = Player.NameClonedPart.Name == Player.name.."'s_clone"

is what i wrote