Hey DevForums,
I’ve been working on a railgun using raycasting. I’ve been trying to make it so if the ray hits an accessory, it’ll make the person wearing it take damage. Unfortunately, it seems to not work. I’ve tried making so if the the ClassName of the object is an accessory, it’ll do damage to the player.
Here is the Script:
local range = 400
local debris = game:GetService("Debris")
local bulletFolder = game.Workspace.Bullets
local function CreateBeam(orgin, direction)
local midpoint = orgin + direction/2
local part = Instance.new("Part")
part.Parent = bulletFolder
part.Anchored = true
part.CanCollide = false
part.Material = Enum.Material.Neon
part.Transparency = 0.4
part.BrickColor = BrickColor.new("Brick yellow")
part.CFrame = CFrame.new(midpoint, orgin)
part.Size = Vector3.new(.25, .25, direction.magnitude)
debris:AddItem(part, 0.15)
end
local function muzzle()
tool.MuzzleFlash.MuzzleEffect.Enabled = true
tool.MuzzleFlash.SpotLight1.Enabled = true
tool.MuzzleFlash.SpotLight2.Enabled = true
tool.MuzzleFlash.SpotLight3.Enabled = true
tool.MuzzleFlash.SpotLight4.Enabled = true
tool.MuzzleFlash.SpotLight5.Enabled = true
wait(0.1)
tool.MuzzleFlash.MuzzleEffect.Enabled = false
tool.MuzzleFlash.SpotLight1.Enabled = false
tool.MuzzleFlash.SpotLight2.Enabled = false
tool.MuzzleFlash.SpotLight3.Enabled = false
tool.MuzzleFlash.SpotLight4.Enabled = false
tool.MuzzleFlash.SpotLight5.Enabled = false
end
local ig = {}
tool.Fire1.OnServerEvent:Connect(function(player, mousePos, orginPos)
local direction = (mousePos - orginPos).Unit*range
local result = workspace:Raycast(orginPos, direction)
if result then
local character = result.Instance.Parent
local humanoid = character:FindFirstChild("Humanoid")
local head = character:FindFirstChild("Head")
if humanoid and humanoid ~= player.Character.Humanoid then
if result.Instance.Name == "Head" then
tool.Handle.Hit:Play()
humanoid:TakeDamage(170)
else
tool.Handle.Hit:Play()
humanoid:TakeDamage(90)
end
if result.Instance.ClassName == "Accessory" then
tool.Handle.Hit:Play()
humanoid:TakeDamage(90)
end
end
end
tool.Fire.Shoot:Play()
CreateBeam(orginPos, direction)
muzzle()
wait(0.2)
tool.Fire.Charge:Play()
end)