Help with spell collision

Well, I’m trying to get the red object to be thrown forward until it touches the green object, when that happens, the red object must stop moving immediately.

The red object is taking too long to stop moving, and I need the object to stop before crossing, because I’m trying to create a spell collision with it.


Screenshot:



Script:

local FX = workspace:WaitForChild("FX")

local rStorage = game:GetService("ReplicatedStorage")
local rEvent = rStorage.Events:WaitForChild("Projectile")

rEvent.OnServerEvent:Connect(function(plr)
	local char = workspace:WaitForChild(plr.Name)
	local hum = char:WaitForChild("HumanoidRootPart")
	
	local x = Instance.new("Part", FX)
	x.Name = "Projectile"
	x.Anchored = false
	x.CanCollide = false
	x.BrickColor = BrickColor.new("Really red")
	x.Material = "Neon"
	x.Shape = "Ball"
	x.Size = Vector3.new(3,3,3)
	x.CFrame = hum.CFrame * CFrame.new(0,0,-3)
	game.Debris:AddItem(x, 15)
	
	local bv = Instance.new("BodyVelocity", x)
	bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	bv.Velocity = hum.CFrame.lookVector * 100
	
	x.Touched:Connect(function(hit)
		if hit.Name == "Barrier" then
			bv.Velocity = Vector3.new()
		end
	end)
end)

Thank you for your attention!

Do you mean WaitForChild:(plr.Character)?

1 Like

Both work. The script is not giving an error, it is just not having the expected result…

What are you exactly trying to get in result?

I would like the projectile to stop moving as soon as it touches the barrier, but at the moment it’s moving a little bit from the position it should stop

Is the spell moving fast? The physics updates at 60 fps, It might be going so fast that it goes through the block or the update happened and the spell is inside the block

Try using raycast. You can make it so that collision occurs when raycast’s length goes under a certain threshold, e.g 0.1 studs.

1 Like

Never use Touched for this type of things, you can use Ray Cast.

Try setting the networkownership of the parts to ‘nil’, this sets it automatically to the server. I hope this helps your problem.

Maybe I’m a little late but use RunService.HeartBeat in order to

  1. move the projectile
  2. send several raycasts to detect if the projectile hit something.

Neither body velocities and tween service are efficient for making projectiles.

:grinning: