Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make it so it takes damage when you hit a "attack"?

So, I made a game, and I wanna know how to make it so everyone which touches it but the player generating it gets hurt. This is the only issue I have before the attack is finalized. I believe the issue is in the part creation script.

Main script part:

local ReplicatedStorage =  game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("forcebrr")

RemoteEvent.OnServerEvent:Connect(function(player)
	local part = game.ServerStorage.ForceField:Clone()
	part.Parent = workspace
	part.Position = player.Character.Torso.Position
	part.CanCollide = false
	part.Anchored = true
	part.Touched:Connect(function(hit)
		local char = player.Character
		local humanoid = char.Humanoid
		hit.Humanoid:TakeDamage(20)
	end)

All other parts are just me making it smoother and resizing it.

1 answer

local ReplicatedStorage =  game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("forcebrr")
local deb = false
local damagecooldown = 1 -- 1 second cooldown
RemoteEvent.OnServerEvent:Connect(function(player)
    local part = game.ServerStorage.ForceField:Clone()
    part.Parent = workspace
    part.Position = player.Character.Torso.Position
    part.CanCollide = false
    part.Anchored = true
    part.Touched:Connect(function(hit)
if deb then return end
 if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then -- checks if player
        local char = hit.Parent -- defines character
if char.Name ~= player then -- checks if it is the same player
deb = true
        local humanoid = char.Humanoid -- defines humanoid
        humanoid:TakeDamage(20) -- damages player
wait(cooldown)
deb = false
end
end
    end)

Mistakes:

    part.Touched:Connect(function(hit)
        local char = player.Character -- the player is the player who generated the forcefield
        local humanoid = char.Humanoid -- youre getting the humanoid of the player who generated
        hit.Humanoid:TakeDamage(20) -- hit is a part like leftfoot so you have to get the parent of it --and then get Humanoid, but if its like a baseplate then it will error, because it has no humanoid
-- so you check if it is a player with getplayerfromcharacter
    end)

3 comments

0

While it works, it also damages and insta kills the player who created it (Aka the player who pressed R.) Can you fix that?

0

yeah !

0

added some changes

This discussion is preserved as it was. New answers, comments, and votes are closed.