top of page

Dealing Damage In Roblox Studio



Depending on the game mechanics you have in your experience there are a number of ways to do damage to your players. The most common is to use the Touched event. When a player touches a specific part we can deal damage to them and use a debounce value to determine how frequently the damage is done. This works fine if the player is moving, however if the player runs into the middle of a lava field and stands still they will stop taking damage until they move again. In most cases this is not how you want your lava to behave.


Continuous Damage Touching A Part

In my video about continuous damage I use a repeat until loop to deal damage to the player and slow the loop using a task.wait(damageRate) statement. The damage rate is a variable I created allowing me to easily change how often the player takes damage.


This loop will continue to run until the condition is met. In the video example I check at the end of each loop whether the player’s humanoid is standing on the FloorMaterial called CrackedLava. CrackedLava is the material I have set on the part, however it could be any material you like. If the FloorMaterial is not CrackedLava then the loop ends and resets the debounce to true so the code can run again when the player touches the lava.


Using An Attribute To Deal Continuous Damage

There may be many ways you want to damage your players in the experiences you develop. In the example below I have added a local script to each character in the game. The script creates a boolean attribute called “canDamage” for each character.


Using the method GetAttributeChangedSignal I connect a function that will damage the player any time that canDamage is set to true. The while loop at the end of the script is just for demonstration and testing.


I use the same variables damageAmt and damageRate to control how often and how much damage is done. Using an attribute in this way allows you as a developer to set canDamage for any character to true in any given situation and they will continue to take damage regardless of what they are doing until canDamage is set to false.


Have fun damaging your players. I hope you find it useful :)

local rs = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local damageRate = 3
local damageAmt = 5

char:SetAttribute("canDamage",false)

char:GetAttributeChangedSignal("canDamage"):Connect(function()
	while char:GetAttribute("canDamage")do
		task.wait(damageRate)
		hum:TakeDamage(damageAmt)
	end
end)
	
while task.wait(15)do
	if char:GetAttribute("canDamage") then
		char:SetAttribute("canDamage",false)
	else
		char:SetAttribute("canDamage",true)
	end
	print("canDamage = ",char:GetAttribute("canDamage"))
end



230 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page