I was making a script that makes an r6 sprite follow u and kill u.
i used the magnitude method and its working but its not killing …
Here’s the script ,I might have made some mistakes too
local hrp = script.Parent:FindFirstChild("HumanoidRootPart")
local hum = script.Parent:FindFirstChild("Humanoid")
local dist = 10
local torso = nil
local target = nil
local function search()
for i,v in pairs(workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart")and v~=script.Parent then
local human = v:FindFirstChild("Humanoid")
torso = v:FindFirstChild("HumanoidRootPart")
if (hrp.Position-torso.Position).Magnitude<dist then
dist = (hrp.Position-torso.Position).Magnitude
target = torso
end
end
end
return target
end
while wait(1) do
local target = search()
if target then
hum:MoveTo(torso.Position)
for i,v in pairs(script.Parent:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(h)
if h:FindFirstChild("Humanoid") then
h:FindFirstChild("Humanoid").Health -= 10
end
end)
end
end
else
hum:MoveTo(hrp.Position+Vector3.new(math.random(-50,50),0,math.random(-50,50)))
end
end
for i,v in pairs(script.Parent:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(h)
if h:FindFirstChild("Humanoid") then
h:FindFirstChild("Humanoid").Health -= 10
end
end)
end
end
You can change that to this since you have a variable that represent your NPC’s HumanoidRootPart
hrp.Touched:Connect(function(hittedPart)
if hittedPart:IsA("BasePart") and hittedPart.Name ~= "Handle" and game.Players:GetPlayerFromCharacter(hittedPart.Parent) ~= nil then
local PlrHum = hittedPart.Parent:FindFirstChild("Humanoid")
PlrHum.Health -= 10
end
end)
Did you get any errors or does it do anything?
Nah the part follows and it is like kissing me
Now I’m on mobile but I remember testing ur code so I’m pretty sure it doesn’t work
Lol and anyways, I think the problem is- wait I forgor
Yea I will wait for someone else
Actually no joke this time, I think the code is fine but I don’t know what making it doesn’t hurt you or something
Idk if you are on pc or you have the time to test it but I just put the script in an r6 character.
If you can test it now it would be good
If not, no problem I will ask someone
I am on personal computer or in short just “PC”. I going to test it so you can do the skill of control c and control v or in short copy and paste it
with your search function, you should put a return after setting the target variable. (do ‘return target’). Would you be able to clarify what error/problem is occurring, and how it is meant to be occurring?
Yep the legendary weapon it is
I already did the return target . The thing is that it is searching and following the player but not killing
After this line; ‘v.Touched:Connect(function(h)’ could you print h
Like I said I’m on mobile I’m not on pc unfortunately
Okay, so I have the a solution to this.
Sorry for late reponse since I was on someone else topic.
Here is it :
local Character = script.Parent
local hrp = script.Parent:FindFirstChild("HumanoidRootPart") -- Don't ask why I forgot to change "script.Parent" to just "Character"
local Debounce = false
hrp.Touched:Connect(function(hittedPart)
if Debounce ~= true then
if hittedPart:IsA("BasePart") and hittedPart.Name ~= "Handle" and game.Players:GetPlayerFromCharacter(hittedPart.Parent) ~= nil then
for _, bodyPart in pairs(Character:GetChildren()) do
if bodyPart:IsA("BasePart") and hittedPart ~= bodyPart then
Debounce = true
local Humanoid = hittedPart.Parent:FindFirstChild("Humanoid")
Humanoid.Health -= 10
task.wait(.25) -- Set how long you wanted the cooldown, like for example 0.15 or .75
Debounce = false
end
end
end
end
end)
hrp.TouchEnded:Connect(function()
if Debounce ~= false then
Debounce = false
end
end)
I can’t test it run did it work ?
And can u explain me with the denounce thing?
I’m just new to scripting
What I mean is explain the code