Problem: So I made a bow and everything works perfect until the ray hits an ignore part. Then it goes in a random direction for no reason.
I realize this question has been solved before but the solution does not seem to work for my problem. This is a local script and is a child of the bow tool. I was not sure if I should include the whole thing but I did just in case. There are no error messages.
Code:
local toolEquipped = false
local bowDrawn = false
local bowDrawnAnimationPlaying = false
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local localPlayer = Players.LocalPlayer
local debounce = false
local char = localPlayer.Character or localPlayer.CharacterAdded:wait()
repeat wait() until char:FindFirstChild("Humanoid")
local humanoid = char:WaitForChild("Humanoid")
local preAnimation = script.Parent:WaitForChild("BowDraw")
local animation = humanoid:LoadAnimation(preAnimation)
local root = char:WaitForChild("HumanoidRootPart")
script.Parent.Activated:Connect(function() --When activated
if debounce == false and bowDrawn == true and toolEquipped == true then
bowDrawn = false
debounce = true
bowDrawnAnimationPlaying = false
animation:Stop()
humanoid.WalkSpeed = 16
local mouse = localPlayer:GetMouse()
local beam = Instance.new("Beam")
beam.Segments = 1
beam.Width0 = 0.2
beam.Width1 = 0.2
beam.Color = ColorSequence.new(Color3.new(242, 243, 243))
beam.FaceCamera = true
-- create attachments
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
beam.Attachment0 = attachment0
beam.Attachment1 = attachment1
-- parent attachments to Terrain
beam.Parent = game.Workspace
attachment0.Parent = workspace.Terrain
attachment1.Parent = workspace.Terrain
-- grab the mouse
root.CFrame = CFrame.new(root.Position, root.Position + mouse.Hit.LookVector)
local ray = Ray.new(script.Parent.Handle.Position, (mouse.Hit.Position - script.Parent.Handle.Position).Unit*1000)
local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent,workspace:WaitForChild("Ignore")})
local finish = pos
local origin = script.Parent.Handle.Position
attachment0.WorldPosition = origin
attachment1.WorldPosition = finish
game.ReplicatedStorage.RemoteEvent:FireServer(hit)
wait(.05)
beam.Transparency = NumberSequence.new(.1,.1)
wait(.05)
beam.Transparency = NumberSequence.new(.2,.2)
wait(.05)
beam.Transparency = NumberSequence.new(.3,.3)
wait(.05)
beam.Transparency = NumberSequence.new(.4,.4)
wait(.05)
beam.Transparency = NumberSequence.new(.5,.5)
wait(.05)
beam.Transparency = NumberSequence.new(.6,.6)
wait(.05)
beam.Transparency = NumberSequence.new(.7,.7)
wait(.05)
beam.Transparency = NumberSequence.new(.8,.8)
wait(.05)
beam.Transparency = NumberSequence.new(.9,.9)
wait(.05)
beam.Transparency = NumberSequence.new(1,1)
beam.Enabled = false
debounce = false
end
end)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.KeyDown:connect(function(key)
if key == "e" and toolEquipped == true then
if bowDrawnAnimationPlaying == false then
animation = humanoid:LoadAnimation(preAnimation)
bowDrawnAnimationPlaying = true
animation:Play()
humanoid.WalkSpeed = 8
wait(.5)
animation:AdjustSpeed(0)
bowDrawn = true
end
end
end)
script.Parent.Equipped:Connect(function()
toolEquipped = true
end)
script.Parent.Unequipped:Connect(function()
toolEquipped = false
if bowDrawnAnimationPlaying == true then
animation:Stop()
bowDrawnAnimationPlaying = false
end
if bowDrawn == true then
bowDrawn = false
end
if humanoid.WalkSpeed < 16 then
humanoid.WalkSpeed = 16
end
end)
humanoid.Died:Connect(function()
char = localPlayer.CharacterAdded:Wait()
humanoid = char:WaitForChild("Humanoid")
if bowDrawnAnimationPlaying == true then
animation:Stop()
bowDrawnAnimationPlaying = false
end
toolEquipped = false
bowDrawn = false
humanoid.WalkSpeed = 16
wait(1.5)
end)
If you need anything else, please ask me for it. Thanks in advance!
