Recently I finished a script that’s very close to being finished. I have everything that I want, though I’ve ran into 2 issues, and I can’t seem to solve them as I’m relatively new to scripting.
1) Player accessories will block the raycast.
For example, if a player was wearing a hat or some sort of back items, the raycast will collide with the accessory instead of dealing damage. I haven’t figured out a way to ignore that, even with some tinkering with the code.
2) Team members take damage.
I got lost when trying to make sure team members can’t take damage from an allies raycast. I tried many things, such as attempting if statements and whatnot but unless I’m doing something horribly wrong (which is probably the case ) then it’ll either deal no damage, or continue damaging an ally.
Here’s the code I currently have so far. It works as is, but as I stated, I’m not sure on how I can fix the issues I’m running into above.
(also this is my first post on the devforum. I apologize for my lack of professionalism! )
Thank you in advance!
local RANGE = 75
local DAMAGE = 15
local HEADSHOT = 30
local Team = game.Teams
game.ReplicatedStorage.RemoteEvents.RPFired.OnServerEvent:Connect(function(Player, TargetLocation, Handle)
if Player.Character == nil then -- Make sure the character exists!
return
end
Handle.Sound:Play()
local Beam = Instance.new("Part", workspace)
Beam.BrickColor = BrickColor.new("Sea green")
Beam.Material = "Neon"
Beam.FormFactor = "Custom"
Beam.Transparency = 0.25
Beam.Anchored = true
Beam.CanCollide = false
--Maths :(
local Distance = (Handle.Position - TargetLocation).magnitude
Beam.Size = Vector3.new(0.45, 0.45, Distance)
Beam.CFrame = CFrame.new(Handle.Position, TargetLocation) * CFrame.new(0, 0, -Distance/2)
game.Debris:AddItem(Beam, 0.2)
--Raycasting
local NewRay = RaycastParams.new()
local RayDirection = (TargetLocation - Handle.Position) * RANGE -- how far you want the ray to travel
NewRay.FilterDescendantsInstances = {Player.Character}
local Result = workspace:Raycast(Handle.Position, RayDirection, NewRay)
if Result then -- if we got result back
if Result.Instance then
-- Something was hit
if Result.Instance.Parent:FindFirstChild("Humanoid") then
Result.Instance.Parent.Humanoid.Health -= DAMAGE
elseif Result.Instance.Parent:FindFirstChild("Head") then
Result.Instance.Parent.Humanoid.Health -= HEADSHOT
end
end
end
end)