I have been trying to figure out why this is only being detected by the part when it is unanchored. The part is a tool, that is suppose to be a vaccum, whenever you hold down your mouse button it spawns a little circle ray thing that is supposed to detect when it hits the other part.
Here is my code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local tool = script.Parent
local equipped = false
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local backPackTrash = ReplicatedStorage:WaitForChild("BackPackTrash")
local beamClone = game.ReplicatedStorage:WaitForChild("ClonedRay"):Clone()
local starterTrashAmount = 5
tool.Equipped:Connect(function()
equipped = true
end)
tool.Unequipped:Connect(function()
equipped = false
end)
mouse.Button1Down:Connect(function()
if equipped then
beamClone.Parent = script.Parent
end
end)
mouse.Button1Up:Connect(function()
if equipped then
beamClone.Parent = nil
end
end)
--variable
local givenCash = false
--Loops through all the trash
for _, trash1 in pairs(game.Workspace.Trash1:GetChildren()) do
--Sets the TargetFilter
mouse.TargetFilter = trash1
if trash1:GetAttribute("Garbage") then -- checks the trash if it's Garbage
--it's garbage
trash1.HitPart.Touched:Connect(function(hit)
print(hit)
if hit.Parent:FindFirstChild("Humanoid") then
print("Going to destroy")
if not mouse.Hit.Position.Magnitude == trash1.HitPart.Position.Magnitude then return end
wait(1)
trash1:Destroy()
if givenCash == false then
givenCash = true
player.leaderstats.Cash.Value += starterTrashAmount
local trash1Clone = backPackTrash.TestBackPackTrash:Clone()
trash1Clone.Parent = player.Character["Starter Backpack"]
trash1Clone.Position = player.Character["Starter Backpack"].Handle.Position + Vector3.new(0,1,0)
end
wait(0.4)
givenCash = false
else
print("Not a valid part")
end
end)
end
end
--Infinite loop
while true do
wait()
--checks if the beamClone is real
if beamClone ~= nil then
--Updates position
beamClone.Position = mouse.Hit.Position
--Some checking if it's in range
if (player.Character:WaitForChild("UpperTorso").Position - mouse.Hit.Position).Magnitude >= 32 then
--not in range, make it vanish
beamClone.Transparency = 1
beamClone.CanTouch = false
else
--in range, make it appear
beamClone.Transparency = 0
beamClone.CanTouch = true
end
end
end