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:
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid")~= nil then hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 20 script.Parent:remove() end 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)
script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid")~= nil then hit.Parent.Humanoid:TakeDamage(20) script.Parent:Destroy() end end)
First create a RemoteEvent
in Workspace and name it "Fired"
Then implement this in the script:
game.Workspace.Fired.OnServerEvent:Connect(function(Player) -- Finds the RemoteEvent local AlreadyTouched = false local Character = Player.Character or Player.CharacterAdded:wait() -- Finds a Player local Fireball = script.Parent -- Finds the Fireball you created Fireball.Touched:Connect(function(Hit) local Humanoid = Hit.Parent:FindFirstChild("Humanoid") --Finds the Player that has "Humanoid" if Humanoid == nil then return end if AlreadyTouched == false then AlreadyTouched = true -- If it touches, then it'll activate this local debris = game:GetService("Debris") if Humanoid then local creatorTag = Instance.new("ObjectValue") -- Creates a tag like a tool creatorTag.Value = Player creatorTag.Name = "creator" creatorTag.Parent = Humanoid debris:AddItem(creatorTag, 1) end; if Humanoid.Parent == Character then Humanoid.Health = Humanoid.Health - 0 else Humanoid.Health = Humanoid.Health - Humanoid.MaxHealth/2 -- Change the damage Fireball:Destroy() -- If it hits the player, it'll disappear (You can delete this if you want to) end end end) end)