#code not working

13 messages · Page 1 of 1 (latest)

edgy wind
#

this script is meant to make its parent part move towards the player, but when it's running the part just falls down into the void. I can't just anchor the part, as then it can't move towards the player.

local part = script.Parent
local speed = 1

local function findNearestPlayer()
local players = game.Players:GetPlayers()
local nearestPlayer = nil
local nearestDistance = math.huge

for _, player in ipairs(players) do
    local character = player.Character
    if character then
        local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
        if humanoidRootPart then
            local distance = (humanoidRootPart.Position - part.Position).magnitude
            if distance < nearestDistance then
                nearestPlayer = player
                nearestDistance = distance
            end
        end
    end
end

return nearestPlayer

end

while true do
local nearestPlayer = findNearestPlayer()
if nearestPlayer then
local character = nearestPlayer.Character
if character then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
local targetPosition = humanoidRootPart.Position
local currentPosition = part.Position
local direction = (targetPosition - currentPosition).unit
local newPosition = currentPosition + direction * speed * workspace.DeltaTime
part.Position = newPosition
end
end
end
wait()
end

lilac fiber
#

If not then I’m impressed

edgy wind
#

no I don't think so

lilac fiber
#

You don’t think so?

edgy wind
#

wait no it does DeltaTime is not a valid member of Workspace "Workspace"

lilac fiber
#

there’s no thinking involved in that question

lilac fiber
#

Oh no nevermind, it’s just some weird discord formatting issue

#

Welp delta time isn’t in workspace

All of the RunService events provide a method to get DeltaTime.

Heartbeat - DeltaTime
Stepped - RunTime, > DeltaTime
RenderStepped - DeltaTime

#

Hope that helped

toxic wyvern
#

Use runservice to get deltatime

#
local part = script.Parent 
local speed = 1

local function findNearestPlayer()
    local players = game.Players:GetPlayers()
    local nearestPlayer = nil
    local nearestDistance = math.huge

    for _, player in ipairs(players) do
        local character = player.Character
        if character then
            local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
            if humanoidRootPart then
                local distance = (humanoidRootPart.Position - part.Position).magnitude
                if distance < nearestDistance then
                    nearestPlayer = player
                    nearestDistance = distance
                end
            end
        end
    end

    return nearestPlayer
end


runService.PostSimulation:Connect(function(deltaTime)
    local nearestPlayer = findNearestPlayer()
    if nearestPlayer then
        local character = nearestPlayer.Character
        if character then
            local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
            if humanoidRootPart then
                local targetPosition = humanoidRootPart.Position
                local currentPosition = part.Position
                local direction = (targetPosition - currentPosition).unit
                local newPosition = currentPosition + direction * speed * deltaTime
                part.Position = newPosition
            end
        end
    end
    wait()
end)