Here is another question... I have now put the code from local script into a script and it won't work for some reason.
Output(no matter the range):
Not in range!
Expected output(when in 10 studs radius):
In range!
If you are confused why is the "while true do" instead of the Tool.Equipped function is because I put another separate script that activates this one under here when the tool is equipped.
while true do
local playerChar = script.Parent.Parent
local player = game.Players:FindFirstChild(playerChar.Name)
local myTorso = playerChar:WaitForChild("HumanoidRootPart")
local myHumanoid = playerChar:FindFirstChildOfClass("Humanoid")
local Tool = playerChar:WaitForChild("Cuffs")
local EveryTime = player.PlayerGui.Every
function findTarget()
local dist = Vector3.new(10,10,10)
local target = nil
for i,v in pairs(game.Workspace.Dummys:GetChildren()) do
if v.Name == "Dummy" then
local human = v:FindFirstChildOfClass("Humanoid")
local torso = v:FindFirstChildOfClass("Part")
if human and torso.Name == "Torso" then
if human.Health > 0 then
if Tool then
local TorsoMag = (Tool.Position - torso.Position).magnitude
if TorsoMag >= dist then
dist = TorsoMag
target = torso
print("Works!")
end
end
end
end
end
return target
end
end
while wait(0.001) do
local torso = findTarget()
if torso then
EveryTime.Enabled = true
print("In range!")
EveryTime.Time.WeTouch.MouseButton1Click:Connect(function(playerY)
if torso then
if script.Parent.Parent == player.Character then
torso.Anchored,torso.CFrame=true,myTorso.CFrame*CFrame.new(0,0,-6)
wait(0.001)
end
else
local targetS = game.Workspace.Dummys:FindFirstChild(torso.Name)
targetS.Torso.Anchored = false
print("Not Humanoid")
end
end)
else
print("Not in range!")
end
end
end
