I am trying to make it so that when an NPC dies, it drops several orbs that move upwards. However, only one of the orbs moves upwards, while the others stay stationary.
Below are the involved scripts and the process I’ve followed:
Video of the Problem:
Orb Script (Movement):
This script is assigned to each orb. Its function is to move the orb upwards (for debugging, it moves upwards, but the real movement should be towards the player):
local Debris = game:GetService("Debris")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ORB = script.Parent
local TargetPlayer
local lastPlayerPosition
local colors = {
["1"] = Color3.new(255, 0, 0),
["2"] = Color3.new(255, 255, 0),
["3"] = Color3.new(255, 0, 255),
["4"] = Color3.new(0, 255, 0),
["5"] = Color3.new(0, 255, 255),
}
RunService.Stepped:Connect(function(deltaTime)
if not TargetPlayer then
local targetUserId = ORB:GetAttribute("TARGET_PLAYER")
if targetUserId then
TargetPlayer = Players:GetPlayerByUserId(targetUserId)
if TargetPlayer then
lastPlayerPosition = TargetPlayer.Character and TargetPlayer.Character.HumanoidRootPart.Position
end
end
else
if TargetPlayer.Character then
local ID = tostring(ORB:GetAttribute("ID"))
if ID then
print("Moving orb", ID)
ORB.Trail.Color = ColorSequence.new(colors[ID] or Color3.new(1, 1, 1))
ORB.Main.Water1.Color = ColorSequence.new(colors[ID] or Color3.new(1, 1, 1))
ORB.Position = ORB.Position + Vector3.new(0, 0.1, 0)
end
end
end
end)
Function that creates and handles orbs:
This function runs when the NPC dies and generates a random number of orbs. It assigns properties like the ID and target player, and then handles the movement of the orbs using a curve path to simulate the fall from the NPC to a random point near the NPC:
function Drops.GiveReward(player:Player)
local UtilsModel = require(player.Backpack.Modules.Utils)
local OrbsNumber = math.random(3, 5)
local EXP_REWARD = math.random(Drops.EXP_REWARD[1], Drops.EXP_REWARD[2])
local EXP_FOREACH = EXP_REWARD / OrbsNumber
local Info = TweenInfo.new(.8, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local orbs = {}
local RunServiceEvent
local function CurvePath(StartPosition:Vector3, EndPosition:Vector3, Height:number)
return function(Time)
local x = (1 - Time) * StartPosition.X + Time * EndPosition.X
local y = (1 - Time) * StartPosition.Y + Time * EndPosition.Y * math.sin(Time * math.pi) * Height
local z = (1 - Time) * StartPosition.Z + Time * EndPosition.Z
return Vector3.new(x, y, z)
end
end
local function CreateTween(Target:BasePart, TargetPosition:Vector3)
return TweenService:Create(Target, Info, {Position = TargetPosition})
end
-- Create the orbs
for i = 1, OrbsNumber do
local OrbModel = ReplicatedStorage.Assets.Drop["Orb Experience"]:Clone()
OrbModel.Position = script.Parent.Parent.HumanoidRootPart.Position
OrbModel:SetAttribute("EXP_REWARD", EXP_FOREACH)
OrbModel:SetAttribute("TARGET_PLAYER", player.UserId)
OrbModel:SetAttribute("ID", i)
OrbModel.Parent = game.Workspace.Drop
table.insert(orbs, OrbModel)
end
print("give reward", orbs)
-- Moving each orb
for _, orb:BasePart in pairs(orbs) do
local StartPosition = orb.Position
local SpawnPosition = UtilsModel.getRandomPositionAround(script.Parent.Parent.HumanoidRootPart.Position, orb, 10)
local Tween = CreateTween(orb, SpawnPosition)
Tween:Play()
local StartTime = tick()
RunServiceEvent = RunService.Heartbeat:Connect(function(deltaTime)
local Time = (tick() - StartTime) / Info.Time
if Time > 1 then
orb.Position = SpawnPosition
return
end
orb.Position = CurvePath(StartPosition, SpawnPosition, 10)(Time)
end)
task.spawn(function()
Tween.Completed:Wait()
RunServiceEvent:Disconnect()
end)
end
end
Details and Clarifications:
- Orb Creation: The orbs are created server-side. The script runs on each orb and changes its color to ensure they have unique colors, but only one orb moves upwards.
- Attempts to solve the issue:
- I tried replacing
RunService.Stepped
with awhile
loop, but the problem persists. - I also tried using
RunService.Heartbeat
, but the orbs still do not move correctly. - The colors for each orb are being correctly assigned and updated, indicating that the orb instances are being created correctly.
Orb Structure:
Thank you to everyone who can help with this issue! If you have any doubts, feel free to ask, and I’ll try to respond as quickly as possible to find a solution.