So as the title implies, I'm trying to create a damage script for my fireball, however, when fired no damage is done to a humanoid. Here is my script:
1 | script.Parent.Touched:connect( function (hit) |
2 | if hit.Parent:FindFirstChild( "Humanoid" )~ = nil then |
3 | hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 20 |
4 | script.Parent:remove() |
5 | end |
6 | end ) |
Respond ASAP, Thanks!
Firstly, never use deprecated functions, read this to know more about deprecated functions and instances. Secondly you should use Humanoid:TakeDamage(20)
as its more efficient and less messy then hit.Parent.Humanoid.Heath = hit.Parent.Humanoid.Health - 20
.
Here is the fixed code (I tested this in studio so I know it works)
1 | script.Parent.Touched:Connect( function (hit) |
2 | if hit.Parent:FindFirstChild( "Humanoid" )~ = nil then |
3 | hit.Parent.Humanoid:TakeDamage( 20 ) |
4 | script.Parent:Destroy() |
5 | end |
6 | end ) |
First create a RemoteEvent
in Workspace and name it "Fired"
Then implement this in the script:
01 | game.Workspace.Fired.OnServerEvent:Connect( function (Player) -- Finds the RemoteEvent |
02 | local AlreadyTouched = false |
03 | local Character = Player.Character or Player.CharacterAdded:wait() -- Finds a Player |
04 | local Fireball = script.Parent -- Finds the Fireball you created |
05 |
06 | Fireball.Touched:Connect( function (Hit) |
07 | local Humanoid = Hit.Parent:FindFirstChild( "Humanoid" ) --Finds the Player that has "Humanoid" |
08 |
09 | if Humanoid = = nil then return end |
10 |
11 | if AlreadyTouched = = false then |
12 | AlreadyTouched = true -- If it touches, then it'll activate this |
13 | local debris = game:GetService( "Debris" ) |
14 | if Humanoid then |
15 | local creatorTag = Instance.new( "ObjectValue" ) -- Creates a tag like a tool |