How to make a mesh part local?

Since the mesh part has an id and I can’t make a new instance using Instance.new, I tried cloning the part but it leads to some issues when the player dies, the issue is that there is sometimes an extra mesh that was cloned, I assume this is because the script runs again when the player dies, so to fix it I deleted the part upon death, but for some reason, there is still an extra mesh about 25 percent of the time. Here is my code:

local part = game.Workspace.Level1.KillPart4:Clone()
local originalpart = game.Workspace.Level1.KillPart4 
part.CanCollide = true
part.Transparency = 0
part.Anchored = false 
part.Name = "b"
part.Parent = workspace
local Physics = game:GetService("PhysicsService")
Physics:SetPartCollisionGroup(part, "ClientParts")
local player = game.Players.LocalPlayer     
local char = player.Character
local hum = char:WaitForChild("Humanoid")
local distance = 35 
local bp = Instance.new("BodyPosition" ) 
local br = Instance.new("BodyGyro" )
bp.Parent = part
br.Parent = part 
bp.Position = part.Position 
br.MaxTorque = Vector3.new(10000000000000,1000000000000,10000000000000)
bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bp.P = 8000
bp.D = 1300    
while true do  
		for i = 0, distance do
			bp.Position = bp.Position + Vector3.new(1,0,0) wait()  
		end   
		for i = 0, distance do
			bp.Position = bp.Position + Vector3.new(-1,0,0)    wait() 
		end 
	hum.Died:Connect(function() 
		originalpart.Parent = workspace.Level1 
		wait() 
		part:Destroy()	
	end)
end  


The local script is inside starter gui, I’m not sure if thats the reason this happens.

1 Like

Maybe just store the MeshPart in ReplicatedStorage, and clone the MeshPart from your LocalScript?

Try this (also put this script inside StarterPlayer > StarterPlayerScripts)

local part = game.Workspace.Level1.KillPart4:Clone()
local originalpart = game.Workspace.Level1.KillPart4 
part.CanCollide = true
part.Transparency = 0
part.Anchored = false 
part.Name = "b"
part.Parent = workspace
local Physics = game:GetService("PhysicsService")
Physics:SetPartCollisionGroup(part, "ClientParts")
local player = game.Players.LocalPlayer     
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local distance = 35 
local bp = Instance.new("BodyPosition" ) 
local br = Instance.new("BodyGyro" )
bp.Parent = part
br.Parent = part 
bp.Position = part.Position 
br.MaxTorque = Vector3.new(10000000000000,1000000000000,10000000000000)
bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bp.P = 8000
bp.D = 1300

hum.Died:Connect(function() 
	originalpart.Parent = workspace.Level1 
	wait()
	part:Destroy()	
end)

while true do
	for i = 0, distance do
		bp.Position = bp.Position + Vector3.new(1,0,0) 
		wait()  
	end   

	for i = 0, distance do
		bp.Position = bp.Position + Vector3.new(-1,0,0)
		wait() 
	end
end

If I put it in replicated storage, won’t the local script just clone the mesh part again from replicated storage when the player dies? What does putting it in replicated storage do?

try checking if the MeshPart was already cloned, something like this

if not MeshPart then -- check if the MeshPart wasn't cloned yet
    ReplicatedStorage.MeshPart:Clone() -- if it wasn't cloned yet, then clone it
end

That seems to have fixed the issue, however placing the script into starter player scripts just makes it so the part never spawns for some reason.

I could also do that but why does putting it in replicated storage help? I haven’t learned about replicated storage yet so I don’t know what putting it there would do.

Well, does the mesh cloning work? if yes, then there’s no need to clone it from ReplicatedStorage

and putting there won’t do anything

It does work the issue was just when the player died the script would run again and cause there to be an extra mesh since I was cloning it in a local script. I think in this situation checking if the meshpart exists would also work, but VeriBaesix’s solution worked.

2 Likes

Alright, good to hear! why does 30 exists