Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How can i make a part go to a player when your next to the part?

Asked by 14 days ago

so im trying to make a script where if you are next to this specific part, lets name it "Sword" then the sword will go to the players position and if it does not touch the player then it will destroy it self, and if it does then the player dies. Here is what i done, i just dont know how to do the move to player when the player is next to the sword part of the script. I think i will need to use magnitude though.

Script:

local Sword = script.Parent
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local Start = Sword.Position
local PartToMove = Sword
local End -- humanoid root part here, but idk if a normal script can have access to the character

local TweenInformation = TweenInfo.new(
        0.62,
        Enum.EasingStyle.Linear,
        Enum.EasingDirection.InOut,
        0,
        false,
        0
)

local EndProperties = {
        Position = End.Position 
}

local TweenToPlay = TweenService:Create(PartToMove, TweenInformation, EndProperties)


local Sword = script.Parent

while true do
    Sword.CFrame = Sword.CFrame * CFrame.fromEulerAnglesXYZ(0, 0.45, 0)
    wait()
end


Sword.Touched:Connect(function(hit)
 local Humanoid = hit.Parent:FindFirstChild("Humanoid")
  if Humanoid then
 Humanoid.Health = 0
 end
end)







-- if the player is 32 studs next the sword, the sword will play the tween.

1 answer

Log in to vote
1
Answered by
imKirda 731 Moderation Voter
14 days ago

Yeah you will have to use magnitude, here is a little example:

local player = "your player"
local character = player.Character or player.CharacterAdded:Wait() -- waits for the character but do it how you want
local hrp = character:WaitForChild("HumanoidRootPart") -- the player's humanoid root part
local part = script.Parent -- part you want to have distance's counted

while true -- example loop
do
    local distance = (part.Position - hrp.Position).Magnitude -- this is how you count distance
    print("Part is "..math.floor(distance).. " studs away!")
    if distance < 32 then -- if part is closer than 32 studs then
        print("Part is closer than 32 studs to you!")
    end
    wait(1)
end

It is an example but if you will play little bit with it you should understand what to do. Hope this helps. :)

0
Thanks! I will try it and change it a bit and play around, thanks for answering! :D AmeerAlM 4 — 14d
Ad

Answer this question