Problem while a shooting a projectile

  1. I Want to make a wand that shoot a projectile.

  2. It works normally on some places and other it just become a freak.

local Character = Player.Character or Player.CharacterAdded:Wait()
	local Handle: Part = Tool.Handle 
	local Direction = (MousePosition - Tool["Build."].Part.Position).Unit
	local Projectile = game.ReplicatedStorage["Visual effects."].Projectile:Clone()
	Projectile.Parent = workspace
	Projectile.Position = Character.Head.Position + Vector3.new(0,0, 4)
	Projectile.CFrame = CFrame.lookAt(Projectile.Position, MousePosition)


	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
	RaycastParameters.FilterDescendantsInstances = {Character}

	local RaycastResult = workspace:Raycast(Projectile.Position, Direction * 500, RaycastParameters)
	
	if RaycastResult then
		local RayCastDistance = RaycastResult.Distance
		local TimeNeeded = (0.7 * RayCastDistance) / 100
		print("Time needed: "..TimeNeeded)
		local TweenInformation = TweenInfo.new(TimeNeeded, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false)
		local TweenConnection = TweenService:Create(Projectile, TweenInformation, {Position = RaycastResult.Position})
		TweenConnection:Play()
		TweenConnection.Completed:Connect(function()
			Projectile:Destroy()
			
		end)
	else
		local TimeNeeded = (0.7 * 500) / 100
		print("Time needed: "..TimeNeeded)
		local TweenInformation = TweenInfo.new(TimeNeeded, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false)
		local TweenConnection = TweenService:Create(Projectile, TweenInformation, {Position = Direction * 500})
		TweenConnection:Play()
		
		TweenConnection.Completed:Connect(function()
			Projectile:Destroy()
		end)
	end

Try to replace Projectile.Position = Tool["Build."].Part.Position + Direction at line 6 since your direction is defined from that position.

For some reason, it worked, there is still a little bit of the problem, but it is working pretty well.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.