Hello guys/girls, so I was trying to make an afterimage of a player when he moves, everything works fine but there’s a couple of problem I just can’t grasp why.
Here’s the script
local Players = game:GetService('Players')
local function setProperties(part, clone)
part.CanCollide = false
part.Anchored = true
part.Transparency = 0.5
part.Material = Enum.Material.Neon
part.Parent = clone
part.Color = Color3.new(0.419608, 0.419608, 0.419608)
end
local function clonePlayer(model, player)
for v,i in pairs(player:GetChildren()) do
if i:IsA('MeshPart') and i.Name ~= "HumanoidRootPart" then
local temp = i:Clone()
setProperties(temp, model)
end
end
model.Parent = workspace
end
local function placeClone(clone, player)
clone:MoveTo(player:GetPrimaryPartCFrame().Position)
end
Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
wait(1)
while wait(0.25) do
local clone = Instance.new('Model')
clonePlayer(clone, char)
placeClone(clone, char)
game.Debris:AddItem(clone, 3)
end
end)
end)
In the script I use a while wait(0.25) do loop to repeat the process of generating an afterimage.
Here what it looks like
Now the first problem is that below wait(0.25), the clone doesn’t move to the player’s location, here’s an example at wait(0)
Second problem is that sometimes, two clones will spawn at the same location for some reason here’s an example
So yeah if you have any idea why it does this well thanks in advance for knowing.
Now the first problem is that below wait(0.25), the clone doesn’t move to the player’s location, here’s an example at wait(0)
This is caused by the variability that wait yields for, it’s a lot less accurate than task.wait.
Second problem is that sometimes, two clones will spawn at the same location for some reason here’s an example
This is because you’re creating a clone every 0.25 seconds and not checking if the character is moving beforehand.
Here’s a script that worked for me.
local Enumeration = Enum
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local Debris = Game:GetService("Debris")
local function OnPlayerAdded(Player)
local function OnCharacterAdded(Character)
Character.Archivable = true
local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid")
while true do
task.wait(0.1)
if not Character.Parent then break end
if Humanoid.MoveDirection.Magnitude == 0 then continue end
local CharacterClone = Character:Clone()
for _, Descendant in ipairs(CharacterClone:GetDescendants()) do
if not (Descendant:IsA("BasePart")) then continue end
Descendant.Anchored = true
Descendant.CanCollide = false
Descendant.Material = Enumeration.Material.Neon
Descendant.Transparency = 0.5
end
CharacterClone:PivotTo(Character:GetPivot())
Debris:AddItem(CharacterClone, 3)
CharacterClone.Parent = Workspace
end
end
Player.CharacterAdded:Connect(OnCharacterAdded)
end
Players.PlayerAdded:Connect(OnPlayerAdded)
I managed to make it work too with a local script instead, the main script sends an event to the local script to activate it and I didn’t find any problem this way.
local Enumeration = Enum
local Game = game
local Workspace = workspace
local Debris = Game:GetService("Debris")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local function OnCharacterAdded(Character)
Character.Archivable = true
local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid")
while true do
task.wait()
if not Character.Parent then break end
if Humanoid.MoveDirection.Magnitude == 0 then continue end
local CharacterClone = Character:Clone()
for _, Descendant in ipairs(CharacterClone:GetDescendants()) do
if not (Descendant:IsA("BasePart")) then continue end
Descendant.Anchored = true
Descendant.CanCollide = false
Descendant.Material = Enumeration.Material.Neon
Descendant.Transparency = 0.5
end
CharacterClone:PivotTo(Character:GetPivot())
task.wait(0.1)
Debris:AddItem(CharacterClone, 3)
CharacterClone.Parent = Workspace
end
end
Player.CharacterAdded:Connect(OnCharacterAdded)
Here’s a local script implementation that doesn’t require a ‘RemoteEvent’, place it inside the ‘StarterPlayerScripts’ container.