Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Damage 5 Every Second?

Asked by 4 years ago
Edited 4 years ago

okay, so im making a game when you touch the brick, it will damage you by 5 every second. but when you stop touching it, it stops damaging you. but my script doesn't stop it from damaging the player AND it doesn't even damage it every second. i cant seem to figure out how to do this though. please help!

function touch(hit)
    while true do
        hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health -5
        wait (1)
    end
end

script.Parent.Touched:connect(touch)
0
try this: sinbadxfan05 3 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

You have had some really strange answers. This is probably the simplest solution:


--Using a debounce so happens once a second: debounce = false --Touched Function: script.Parent.Touched:Connect(function(hit) --Checks if it is a human: if hit.Parent:FindFirstChild("Humanoid") then --Checks if the debounce is true (returns if it is) if debounce == true then return end --Sets the debounce to true (as now touching): debounce = true --Changes health to current health - 5 hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 5 --Waiting Time: wait(1) --Waits a second and runs again. debounce = false end end)

Added comments within the script but if you have any questions let me know.

0
Works! TheEmeraIdDev 22 — 4y
Ad
Log in to vote
-1
Answered by 4 years ago

try this: ` local function getPlayerFromCharacter(character) for _, player in pairs(game:GetService("Players"):GetPlayers()) do if player.Character == character then return player end end end

script.Parent.Touched:connect(function(hit) if hit and hit.Parent then Character = hit.Parent player = getPlayerFromCharacter(hit.Parent) if player == nil then print("reward: player does not exist") else player.Character.Health = player.Character.Health - 5 wait(1) end end end)

`

0
Nope didnt work, it wouldnt even lower the players hp TheEmeraIdDev 22 — 4y

Answer this question