How would I go about dealing damage to a Wall when a punch anim is played like in this game?
People would make a wall that Contains a Property as Health
, where you can give something Custom Health, If you want to create an Attribute Manually, go to the Very Bottom of the Properties Menu, and Down to the Attributes menu, and Create two Attributes called Health
, and MaxHealth
.
If you want to create it by script, you will use the SetAttribute
function, if there is no Attribute under that specific name, it will create one:
BasePart:SetAttribute("MaxHealth", 100) -- sets 'MaxHealth' Attribute
BasePart:SetAttribute("Health", BasePart:GetAttribute("MaxHealth")) -- Sets Attribute using 'MaxHealth' Attribute
If you really wanted to, you can apply a Tag to Keep track of Walls, using CollectionService
.
To Raycast, we can then use the Direction the Player is Facing, with their HumanoidRootPart
as the Starting point, and theit LookVector
as their Direction, but when you make Raycasts, you have to have a Range to it, which is why we Multiple the Direction.
While being Optional, Its still Recommended that you use RaycastParams
to Filter, and Modify your Raycast, In this case, we would be filtering the Player’s Character.WIth this System, You can use a RemoteEvent
to fire from Client to Server, but Keep in mind that this would make your code insecure, and you would need to secure it with all is called Sanity Checks.
remoteEvent.OnServerEvent:Connect(function(player, range, damage)
-- 'player' is our Player
-- 'range' is how much we want it to go
-- 'damage' is how much damage we deal
local params = RaycastParams.new() -- RaycastParams
params.FilterDescendantsInstances = {player.Character} -- Filter out our Character
params.FilterType = Enum.RaycastFilterType.Blacklist -- sets it so it ignores the Items
local hrp = p.Character:FindFirstChild("HumanoidRootPart") -- looks for HumanoidRootPart
if not hrp then return end -- ends if the HumanoidRootPart is not found
local result = workspace:Raycast(
-- First Argument is the Position, where the ray will start
-- Second Argument is the Direction, where the ray will go
-- Third Argument is the RaycastParams, the Optional bit
hrp.Position, -- the Position of the Humanoid
hrp.CFrame.LookVector * range, -- the way the Character is facing times the range
params -- the RaycastParams
)
And when the Raycast is successful, we can then check if they have these properties to “deal them damage”
if result then -- If the Raycast was a Success
local Item = result.Instance -- the Object we Hit
local health = item:GetAttribute("Health") -- Gets the 'Health' Attribute
if Health then -- if the Attribute Exists in the Object
local maxHealth = item:GetAttribute("MaxHealth")
local dmgResult = math.clamp(health - damage, 0, maxHealth) -- sets a limit to how much you can deal, while dealing damage
item:SetAttribute("Health", dmgResult) -- subtracts the current Amount with the damage result
if item:GetAttribute("MaxHealth") == 0 then -- if the Wall is 'Dead'
-- Do whatever here
end
end
end
This can also be done with ValueBase
related Items.
This is Typically how its done, there are other methods, but this would be a common way of doing it, and more so a simple one.
like this?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PunchEvent = game.ReplicatedStorage.Storage.Remotes.Punch
local Zone1Parts = game.Workspace.Map.Zone1.Model
PunchEvent.OnServerEvent:Connect(function(player, range, damage)
-- 'player' is our Player
-- 'range' is how much we want it to go
-- 'damage' is how much damage we deal
for _, v in pairs (Zone1Parts:GetDescendants())do
Zone1Parts:GetChildren():SetAttribute("MaxHealth", 100) -- sets 'MaxHealth' Attribute
Zone1Parts:GetChildren():SetAttribute("Health", Zone1Parts:GetChildren():GetAttribute("MaxHealth")) -- Sets Attribute using 'MaxHealth' Attribute
end
local params = RaycastParams.new() -- RaycastParams
params.FilterDescendantsInstances = {player.Character} -- Filter out our Character
params.FilterType = Enum.RaycastFilterType.Blacklist -- sets it so it ignores the Items
local hrp = p.Character:FindFirstChild("HumanoidRootPart") -- looks for HumanoidRootPart
if not hrp then return end -- ends if the HumanoidRootPart is not found
local result = workspace:Raycast(
-- First Argument is the Position, where the ray will start
-- Second Argument is the Direction, where the ray will go
-- Third Argument is the RaycastParams, the Optional bit
hrp.Position, -- the Position of the Humanoid
hrp.CFrame.LookVector * range, -- the way the Character is facing times the range
--params -- the RaycastParams
if result then -- If the Raycast was a Success
local Item = result.Instance -- the Object we Hit
local health = item:GetAttribute("Health") -- Gets the 'Health' Attribute
if Health then -- if the Attribute Exists in the Object
local maxHealth = item:GetAttribute("MaxHealth")
local dmgResult = math.clamp(health - damage, 0, maxHealth) -- sets a limit to how much you can deal, while dealing damage
item:SetAttribute("Health", dmgResult) -- subtracts the current Amount with the damage result
if item:GetAttribute("MaxHealth") == 0 then -- if the Wall is 'Dead'
-- Do whatever here
end
end
end
end)