Hi i made a script where it runs multiple stuffs when playerAdded, characterAppearanceLoaded, remoteEvent onServerEvent.
The problem is, after the player dies, the folder is destroyed as expected. But when the remoteEvent gets fired again(after the player’s death), another folder should be created but instead theres TWO created folders, and so goes on by adding +1 folder each time the player dies and fires the event again which isnt what i want.(1,2,3,4,5 folders, etc)
local script: (textbutton)
local rm = game.ReplicatedStorage.wormRemotes.addWorm
script.Parent.MouseButton1Click:Connect(function()
rm:FireServer()
end)
server script:
local PhysicsService = game:GetService("PhysicsService")
local obstacles = "obstacles"
local pass = "pass"
PhysicsService:CreateCollisionGroup(obstacles)
PhysicsService:CreateCollisionGroup(pass)
PhysicsService:CollisionGroupSetCollidable(pass, obstacles, false)
local rm = game.ReplicatedStorage.wormRemotes.addWorm
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAppearanceLoaded:Connect(function(character)
rm.OnServerEvent:Connect(function()
local folder = Instance.new("Folder") --line where creates folder
folder.Parent = workspace
folder.Name = player.Name.."'s Folder"
-- from this line, its just some stuffs i made
for i,v in pairs(character:GetDescendants()) do
if v:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(v, pass)
v.Transparency = 1
else
if v:IsA("Decal") then
v.Transparency = 1
end
end
end
local start = Instance.new("Part", workspace)
start.Name = player.Name.."'s followPart"
start.Transparency = 1
start.CanCollide = false
start.Anchored = false
start.Massless = true
start.CFrame = character.HumanoidRootPart.CFrame
PhysicsService:SetPartCollisionGroup(start, obstacles)
local weld = Instance.new("Weld", start)
weld.Part0 = start
weld.Part1 = character.HumanoidRootPart
weld.C0 = CFrame.new(0,2,0)
local finish = start
character.Head.Position = start.Position
-- end of stuffs i made
character.Humanoid.Died:Connect(function(player) --line where destroys folder
folder:Destroy()
start:Destroy()
end)
end)
end)
end)