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
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
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)
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
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)
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)
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