Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Magnitude check returning item nearby even though its not?

Asked by
Azuc 112
4 years ago
Edited 4 years ago

It's supposed to make the NPC check if food is nearby, however when I move the food out of the workspace or delete it and call the function again it still says there is food nearby. Can't figure out why, I would appreciate some help with this issue!

function FindResource(Type)
    local Head = script.Parent.Head
    for i, v in pairs(game.Workspace.Resources:GetChildren()) do
        if v then
        if v.Name == Type then
            if (v.Position - Head.Position).magnitude <= 50 then
                print("Found Food")
                end
            end
        end
    end
end
0
This script in the NPC is a server script, right? Are you deleting things locally from the client context? If so, they're still there on the server. EmilyBendsSpace 1025 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Works fine for me, I did modify your code somewhat just to make it easier to test.

function FindResource(Type)
    local Head = game.Players.LocalPlayer.Character.Head
    for i, v in pairs(game.Workspace.Resources:GetChildren()) do
        if v then
        if v.Name == Type then
            if (v.Position - Head.Position).magnitude <= 50 then
                print("Found Food"..(v.Position - Head.Position).magnitude)
                end
            end
        end
    end
end

function key(inputObject,gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.Z then
        FindResource("Part")
    end
end

game:GetService("UserInputService").InputBegan:Connect(key)
Ad

Answer this question