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

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

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

✓ Accepted answer

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. :)

1 comment

0

Thanks! I will try it and change it a bit and play around, thanks for answering! :D

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