Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Magnitude check returning item nearby even though its not?

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

1 comment

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.

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.