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

Trying to make the player take damage when function calculates the players head is on fire?

My problem is that I'm trying to call a function when the players head is on fire but I can't figure out how to pass variables on from functions. The fire does make the players head its Parent, so the first function works. I've tried return with no luck. Any ideas? Also need player to take damage while not touching block.

local object = script.Parent

local firePlayer = game.ReplicatedStorage.Fire

function fireTheGui(hit)
	local playersHead = hit.Parent.Head
	local player = hit.Parent.HumanoidRootPart
	local playersHealth = hit.Parent.Humanoid
	if player then
		firePlayer.Parent = playersHead
	end
end

object.Touched:Connect(fireTheGui)

function playerDamage() -------- My problem is here and Below
	if playersHead.Fire then
		playersHealth:TakeDamage(2)
		print("This player is taking Damage")
	end
end

2 answers

✓ Accepted answer

You can pass variable from one to another function.

Example:

function one()
    local number = 10 + 1
    print(two(number)) -- prints 12
end


function two(number)
    return number + 1 -- 11 + 1
end

So in your script you can do the same!

local object = script.Parent

local firePlayer = game.ReplicatedStorage.Fire

function fireTheGui(hit)
    local playersHead = hit.Parent.Head
    local player = hit.Parent.HumanoidRootPart
    local playersHealth = hit.Parent.Humanoid

    if player then
        firePlayer.Parent = playersHead
        playerDamage(playersHead, playersHealth) -- pass your variables
    end
end

object.Touched:Connect(fireTheGui)

function playerDamage(playersHead, playersHealth) -- retrieve your variables
    if playersHead.Fire then
        playersHealth:TakeDamage(2)
        print("This player is taking Damage")
    end
end

Also there's another way doing this with local function: With my understanding local function allows another function to read all non-local variables inside it.

function two()
	return num
end

local function one()
	num = 100 -- non-local variable
	print(two()) -- prints 100
end

one()

So in your script:

local object = script.Parent

local firePlayer = game.ReplicatedStorage.Fire

local function fireTheGui(hit)
    playersHead = hit.Parent.Head
    local player = hit.Parent.HumanoidRootPart
    playersHealth = hit.Parent.Humanoid

    if player then
        firePlayer.Parent = playersHead
        playerDamage() -- pass your variables
    end
end

object.Touched:Connect(fireTheGui)

function playerDamage()
    if playersHead.Fire then
        playersHealth:TakeDamage(2)
        print("This player is taking Damage")
    end
end

ALSO if you want to make the player takes damage while the fire is still on their head then do this:

function playerDamage()
    if playersHead.Fire then
        while wait() do
            playersHealth:TakeDamage(2)
            if playersHead:FindFIrstChild("Fire") == false then
                break
            end
        end
    end
end

----- OR

function playerDamage()
    if playersHead.Fire then
        repeat wait()
            playersHealth:TakeDamage(2)
        until playersHead:FindFirstChild("Fire") == false
    end
end

4 comments

0

But I would like the player to take damage while not touching the block

0

I should've mentioned that

0

I edited it!

0

Nailed it man. Mind if I send you a friends req on roblox?

Sorry for taking long!

Try this code instead

local object = script.Parent

local firePlayer = game.ReplicatedStorage.Fire



local function fireTheGui(hit)
	humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

	if humanoid then
		playersHead = hit.Parent.Head
		
		local FireClone = firePlayer:Clone()
		FireClone.Parent = playersHead

		playerDamage(2)
	end
end

object.Touched:Connect(fireTheGui)

function playerDamage(dmg)
	if playersHead.Fire then
		repeat	humanoid:TakeDamage(dmg)
			wait(1)
		until
		humanoid.Health == 0
	end
	return true
end

5 comments

0

I have a problem with the script

0

The player needs to be damaged when not touching the pad

0

Make a repeat loop or while loop

0

okay

0

i replaced the playerDamage function

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