Hello Devs, I’m trying to make a shard that will kill you when you close to it and set a part to the player position with while loop, I’m using magnitude.
but it’s not working. No Errors. Nothing.
while true do -- Making The Shard Rotate
script.Parent.Shard.CFrame = script.Parent.Shard.CFrame * CFrame.fromEulerAnglesXYZ(.0,.1,.0) -- Spinning Part
wait()
end
-----------------------------------------------------------------
-- Magnitude // Particle to HumanoidRootPart
local mainPart = script.Parent.Shard -- the shard
local function findTarget()
local dist = 20
local target
for i, v in pairs(game.Workspace:GetChildren()) do -- Finding the player
local human = v:FindFirstChild("Humanoid")
local humanoidRoot = v:FindFirstChild("HumanoidRootPart")
if human and humanoidRoot and v ~= script.Parent then
if (mainPart.Position - humanoidRoot.Position).magnitude < dist then -- magnitude part
target = humanoidRoot -- target to player humanoid root part
-- // Setting Particle to RootPart
while wait() do
game.Workspace.Core.Position = target.Position -- Setting the part/particle to the player position
end
-- // Taking Damage
while wait(1.5) do
human:TakeDamage(10) -- Takes player damage every 1.5 second close to the shard
end
end
end
end
end
Your issue is due to the while loop being at the top of the script. That loop never ends, and all of the code (assuming this is one big script) is running on a single ‘thread’ (process of execution), so the below code will never run as your while loop at the top runs forever.
If these are multiple scripts then I stand corrected.
Its not working because when you call a while loop it will keep run the code inside of it, and not running the code that is after the loop. Use Spawn() to run several loops at once.
spawn(function() while wait() do print("Loop1 Ran") -- Prints this end end)
while wait() do print("Loop2 Ran")-- Prints this, means that both loops are running. end
spawn(function() while wait(0.1) do -- Making The Shard Rotate script.Parent.Shard.CFrame = script.Parent.Shard.CFrame *CFrame.fromEulerAnglesXYZ(.0,.1,.0) -- Spinning Part wait() end end)
-- Magnitude // Particle to HumanoidRootPart
local mainPart = script.Parent.Shard -- the shard
local function findTarget() local dist = 20 local target for i, v in pairs(game.Workspace:GetChildren()) do -- Finding the player local human = v:FindFirstChild("Humanoid") local humanoidRoot = v:FindFirstChild("HumanoidRootPart") if human and humanoidRoot and v ~= script.Parent then
if (mainPart.Position - humanoidRoot.Position).magnitude < dist then -- magnitude part
target = humanoidRoot -- target to player humanoid root part
-- // Setting Particle to RootPart
spawn(function()
while wait() do
game.Workspace.Core.Position = target.Position -- Setting the part/particle to the player position
end
end)
-- // Taking Damage
while wait(1.5) do
human:TakeDamage(10) -- Takes player damage every 1.5 second close to the shard
end
end
end
end
Also, rotate the shard in a seperate script, using while loops is expensive, so don’t overuse them.
There is an error within your code, apparently you forgot to put the last end to close the function.
local mainPart = script.Parent.Shard -- the shard
local function findTarget()
local dist = 20
local target
for i, v in pairs(game.Workspace:GetChildren()) do -- Finding the player
local human = v:FindFirstChild("Humanoid")
local humanoidRoot = v:FindFirstChild("HumanoidRootPart")
if human and humanoidRoot and v ~= script.Parent then
if (mainPart.Position - humanoidRoot.Position).magnitude < dist then -- magnitude part
target = humanoidRoot -- target to player humanoid root part
-- // Setting Particle to RootPart
spawn(function()
while wait() do
game.Workspace.Core.Position = target.Position -- Setting the part/particle to the player position
end
end)
-- // Taking Damage
while wait(1.5) do
human:TakeDamage(10) -- Takes player damage every 1.5 second close to the shard
end
end
end
end
end