Raycast glitching out badly

Hey there. I am trying to make an attack for my game using raycasts where if the Boss is detected through the camera, you will cut through the boss. However, whenever I try it out, I get teleported into the baseplate and my Camera is super zoomed out, and I can’t even zoom back in. Thank you for your help in advance.

wait(.5)
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://15268336493"
local loadedslash = humanoid:LoadAnimation(slash)
local stunned = player.Character:GetAttribute("Stunned")
local cutp1 = Instance.new("Animation")
cutp1.AnimationId = "rbxassetid://16370861710"
local loadedcutp1 = humanoid:LoadAnimation(cutp1)
local cutp2 = Instance.new("Animation")
cutp2.AnimationId = "rbxassetid://16370974725"
local loadedcutp2 = humanoid:LoadAnimation(cutp2)

game.UserInputService.InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if stunned == false or stunned == nil then
		player.Character.Humanoid.WalkSpeed = 20
		end
	end
end)



game.UserInputService.InputEnded:Connect(function(input, gpe)

	
	
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local debouncewait = false
		local debounce = 2
		if debouncewait == false and stunned == false or debouncewait == false and stunned == nil  then
			debouncewait= true
			print(stunned)

			print("clicked")
			 
			loadedslash:Play()
            
			game.ReplicatedStorage.slashsfx:Play()
			game.ReplicatedStorage.slash1remote:FireServer()
			wait(0.25)
			game.ReplicatedStorage.slashsfx:Play()
			game.ReplicatedStorage.slash2remote:FireServer()
			wait(0.25)
			game.ReplicatedStorage.slashsfx:Play()
			game.ReplicatedStorage.slash3remote:FireServer()


				wait(debounce)
			debouncewait = false


			end

			
	end
	if input.KeyCode == Enum.KeyCode.E then
		local debouncewait = false
		local debounce = 0
		if stunned == false and debouncewait == false or stunned == nil and debouncewait == false then
			loadedcutp1:Play()
			local camera = workspace.CurrentCamera
			local rayorigin = camera.CFrame.Position
			local rayDirection = camera.CFrame.LookVector * 100000
			local rayparams = RaycastParams.new()
			rayparams.FilterDescendantsInstances = {player.Character:GetChildren(), player.Character.sword}
			rayparams.FilterType = Enum.RaycastFilterType.Exclude
			wait(1.5)
			local rayresults = workspace:Raycast(rayorigin, rayDirection, rayparams)
			if rayresults then
				print(rayresults.Instance, rayresults.Instance.Parent)
				if rayresults.Instance.Parent or rayresults.Instance.Parent.Parent == workspace.BossModel then
					loadedcutp2:Play()
	
					player.Character.HumanoidRootPart.Position = workspace.BossModel.Torso.Position 
					end
					else
				print("no results")
			end
		end
	end
	
	if input.KeyCode == Enum.KeyCode.LeftShift then
		player.Character.Humanoid.WalkSpeed = 16
	end

end)

2 Likes

Sorry about that. As I said in the previous message, scripting Raycast can be a pain in the bum, but usually the code tends to be wrong because we are so dedicated to making it work that we don’t see the issues in our code. But I’ve think I managed to help you out, hopefully lol

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://15268336493"
local loadedSlash = humanoid:LoadAnimation(slash)

local cutp1 = Instance.new("Animation")
cutp1.AnimationId = "rbxassetid://16370861710"
local loadedCutp1 = humanoid:LoadAnimation(cutp1)

local cutp2 = Instance.new("Animation")
cutp2.AnimationId = "rbxassetid://16370974725"
local loadedCutp2 = humanoid:LoadAnimation(cutp2)

local stunned = player.Character:GetAttribute("Stunned")

game.UserInputService.InputBegan:Connect(function(input, gpe)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if stunned == false or stunned == nil then
            player.Character.Humanoid.WalkSpeed = 20
        end
    end
end)

game.UserInputService.InputEnded:Connect(function(input, gpe)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        local debouncewait = false
        local debounce = 2
        if not debouncewait and (stunned == false or stunned == nil) then
            debouncewait = true
            print("clicked")
            loadedSlash:Play()
            game.ReplicatedStorage.slashsfx:Play()
            game.ReplicatedStorage.slash1remote:FireServer()
            wait(0.25)
            game.ReplicatedStorage.slashsfx:Play()
            game.ReplicatedStorage.slash2remote:FireServer()
            wait(0.25)
            game.ReplicatedStorage.slashsfx:Play()
            game.ReplicatedStorage.slash3remote:FireServer()
            wait(debounce)
            debouncewait = false
        end
    end
    
    if input.KeyCode == Enum.KeyCode.E then
        local debouncewait = false
        local debounce = 0
        if not debouncewait and (stunned == false or stunned == nil) then
            debouncewait = true
            loadedCutp1:Play()
            local camera = workspace.CurrentCamera
            local rayorigin = camera.CFrame.Position
            local rayDirection = camera.CFrame.LookVector * 100000
            local rayparams = RaycastParams.new()
            rayparams.FilterDescendantsInstances = {player.Character, player.Character.sword}
            rayparams.FilterType = Enum.RaycastFilterType.Blacklist
            wait(1.5)
            local rayresults = workspace:Raycast(rayorigin, rayDirection, rayparams)
            if rayresults and (rayresults.Instance.Parent == workspace.BossModel or rayresults.Instance.Parent.Parent == workspace.BossModel) then
                loadedCutp2:Play()
                humanoid.RootPart.CFrame = CFrame.new(workspace.BossModel.Torso.Position)
            else
                print("no results")
            end
            wait(debounce)
            debouncewait = false
        end
    end
    
    if input.KeyCode == Enum.KeyCode.LeftShift then
        player.Character.Humanoid.WalkSpeed = 16
    end
end)

Did everything turn out the way you wanted it to be?

Thank you! I’m not that good with Cframes and stuff lol so that’s probably where I messed up but thanks for the help I appreciate it!

Of course, Man. No problem and I’m down to help anytime if you have issues with code

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