The wand tool I’ve created isn’t working correctly. I was wondering if anyone could help me figure out why I’ve created a print for the mouse click (which says it’s successful); however, when the mouse is clicked the ray isn’t appearing or doing damage to NPCs.
Here’s the script:
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
print("Mouse pressed")
local ray = Ray.new(tool.WandHead.CFrame.Position (mouse.Hit.Position - tool.WandHead.CFrame.Position).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Pink")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = .25
beam.Anchored = true
beam.CanCollide = false
local distance = (tool.WandHead.CFrame.Position - position).magnitude
beam.Size = Vector3.new(.3, .3, distance)
beam.CFrame = CFrame.new(tool.WandHead.Position, position) * CFrame.new(0, 0, - distance / 2)
game:GetService("Debris"):AddItem(beam, .1)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
humanoid:TakeDamage(30)
end
end
end)
end)
Let me know if you see where I’ve made my mistake! I haven’t been able to figure it out.
The output says " Players.Auevi.Backpack.Wand.LightningScript:8: attempt to call a Vector3 value " but I’m just not sure how to fix that (it works in all other parts of the script besides on Line 8.
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
tool.Activated:Connect(function()
print("Mouse pressed")
local ray = Ray.new(tool.WandHead.CFrame.Position, (mouse.Hit.Position - tool.WandHead.CFrame.Position).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Pink")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = .25
beam.Anchored = true
beam.CanCollide = false
local distance = (tool.WandHead.CFrame.Position - position).magnitude
beam.Size = Vector3.new(.3, .3, distance)
beam.CFrame = CFrame.new(tool.WandHead.Position, position) * CFrame.new(0, 0, - distance / 2)
game:GetService("Debris"):AddItem(beam, .1)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
humanoid:TakeDamage(30)
end
end
end)
Thank you so much The script works now! I greatly appreciate it!
As for the damage script, should I put a regular script (not local) into the tool? or use a RE?
Here’s what I’ve done so far for the damage script:
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local ray = Ray.new(tool.WandHead.CFrame.Position, (mouse.Hit.Position - tool.WandHead.CFrame.Position).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
humanoid:TakeDamage(30)
end
end