I’m making a bow for my game that will turn players into an npc that chases people. since i couldn’t get pathfinding to work on players i decided to just clone them and temporarely teleport the target player away. however the task.delay I used doesn’t seem to work:
here is my code ( everything works perfectly except for the teleporting back)
local tool = script.Parent
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
tool.RemoteEvent.OnServerEvent:Connect(function(player,Hit)
script.Parent.Arrow:Destroy()
task.delay(30,function()
script.Parent:Destroy()
end)
local character = player.Character
if not character then return end
local origin = character:FindFirstChild("Head") and character.Head.Position or character:GetPivot().Position
local direction = (Hit - origin).Unit * 1000
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local result = workspace:Raycast(origin, direction, raycastParams)
local hitPosition = result and result.Position or (origin + direction)
task.spawn(function()
local Enemy: Model = result.Instance.Parent
if Enemy:FindFirstChild('Humanoid') then
Enemy.Archivable = true
local EnemyPlayer = game.Players:GetPlayerFromCharacter(Enemy)
local Clone = Enemy:Clone()
Enemy.Humanoid.WalkSpeed = 0
Enemy.HumanoidRootPart.CFrame = workspace.CupidPart.CFrame + Vector3.new(0,10,0)
local root = Clone.HumanoidRootPart
Clone.Parent = workspace
local ZombieAIModule = require(game.ServerScriptService.ZombieAI)
ZombieAIModule.Infect(Clone,player)
task.spawn(function()
task.delay(30,function()
Enemy.HumanoidRootPart.CFrame = Clone.HumanoidRootPart.CFrame
Clone:Destroy()
Enemy.Parent = workspace
end)
end)
end
end)
-- Create attachments
local attachment0 = Instance.new("Attachment")
attachment0.WorldPosition = origin
attachment0.Name = "BeamStart"
attachment0.Parent = workspace.Terrain
local attachment1 = Instance.new("Attachment")
attachment1.WorldPosition = hitPosition
attachment1.Name = "BeamEnd"
attachment1.Parent = workspace.Terrain
-- Create beam
local beam = Instance.new("Beam")
beam.Attachment0 = attachment0
beam.Attachment1 = attachment1
beam.Color = ColorSequence.new(Color3.fromRGB(255, 105, 180)) -- Hot pink
beam.Width0 = 0.2
beam.Width1 = 0.2
beam.FaceCamera = true
beam.LightInfluence = 0
beam.Transparency = NumberSequence.new(0)
beam.Parent = workspace.Terrain
-- Fade out over 3 seconds
local fadeTime = 3
local startTime = tick()
local connection
connection = RunService.Heartbeat:Connect(function()
local elapsed = tick() - startTime
local alpha = math.clamp(elapsed / fadeTime, 0, 1)
beam.Transparency = NumberSequence.new(alpha)
if alpha >= 1 then
connection:Disconnect()
attachment0:Destroy()
attachment1:Destroy()
beam:Destroy()
end
end)
end)