I have watched tons of videos and none of them explain it towards a beginner point of view. Does anyone have an easier way to understand it.
The steps for ranged weapons are usually as follows:
- Weapon attacks
- Raycasts and checks for player in path
- Humanoid:TakeDamage()
Let's say I have a gun:
tool.Activated:Connect(function() local rayOrigin = endOfBarrel.Position local rayDirection = part.CFrame.LookVector * 1000 local ray = workspace:Raycast(rayOrigin, rayDirection) if ray.Instance and ray.Instance.Parent and ray.Instance.Parent:FindFirstChild("Humanoid") then ray.Instance.Parent.Humanoid:TakeDamage(10) end end
Basically it all boils down to knowing how to retrieve what you have hit. This is very different based on whether the weapon is ranged, melee, psychic or whatever.
-
Here's a link on raycasting which should help clear some of the confusing functions.
This is the script of a melee weapon
local tool = script.Parent local canDamage = false local canSwing = true local function onTouch(otherPart) local humanoid = otherPart.Parent:FindFirstChild("Humanoid") if not humanoid then -- this is use to find did the too touch a humanoid return end if humanoid.Parent ~= tool.Parent and canDamage then humanoid:TakeDamage(35) -- here change the weapon damage else return end canDamage = false end local function slash() if canSwing then -- here is for playinf the slash animation canSwing = false local str = Instance.new("StringValue") str.Name = "toolanim" str.Value = "Slash" str.Parent = tool canDamage = true wait(1) --cooldown time canSwing = true end end tool.Activated:Connect(slash) tool.Handle.Touched:Connect(onTouch)