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

How do I check if a player has died?

toggle3 = true
script.Parent.Touched:Connect(function(hit)
	if	hit.Parent:FindFirstChild('Humanoid')  then
		if hit.Parent:FindFirstChild("Humanoid").Health = 0 then
			local Sword = game.ServerStorage.ClassicSword:Clone()
			toggle3 = false
			Sword.Parent = hit.Parent
end
end)

Error : Workspace.Part.Script:22: 'then' expected near '='

3 comments

0

This would not be a good idea for the system he wants to do, he wants to detect if the player dies on the block. thus giving the tool. with .Died if player touch button and the player leaves the block and dies he will receive the tool. with .Health he'd only receive the tool if he died on the block.

0

I didn't understand his description as it said error on line 22, so I just answered his title question.

3 answers

✓ Accepted answer
toggle3 = true
script.Parent.Touched:Connect(function(hit)
    if  hit.Parent:FindFirstChild('Humanoid')  then
        if hit.Parent:FindFirstChild("Humanoid").Health == 0 then --when comparing use 2 '=' signs
            local Sword = game.ServerStorage.ClassicSword:Clone()
            toggle3 = false
            Sword.Parent = hit.Parent
end
end)

Hope this helps. Accept the answer if it worked.

Do not use if ... = ... then use if ... == ... then

Examples of Conditional Statements:

-- 1
if 2 + 3 == 5 then
	print("two plus three is five")
end

-- 2
if 2 + 3 == 5 then print ("2+3==5") end
if 5 > 2 then print("5>2") end
if 2 < 5 then print("2<5") end
if 5 >= 5 then print("5>=5") end
if 5 <= 5 then print("5<=5") end
if 1 ~= 100 then print("1~=100") end
if "some text" then print("It worked") end

-- 3
if 10 > 100 then 
    print("10 is greater than 100") 
else 
    print("10 is less than 100") 
end

-- 4
if 10 > 100 then 
    print("10 is greater than 100") 
elseif 10 > 50 then	
    print("10 is greater than 50") 
elseif 10 < 100 then	
    print("10 is less than 100") 
end

-- 5
local pizzaPlaceIsOpen = false
local tacoPlaceIsOpen = true
local hotdogStandIsOpen = false

if pizzaPlaceIsOpen == true then
    print("Pizza Place")
elseif tacoPlaceIsOpen then  -- var == true is usually unnecessary when var is a boolean
    print("Taco Place")
elseif hotdogStandIsOpen then
    print("Hotdog Stand")
else
    print("Hungry!")
end

You can find this in the roblox wiki.

Here is fixed script:

local toggle3 = true

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and toggle3 == false then -- you forgot to add toggle3 == false
		if hit.Parent:WaitForChild("Humanoid").Health == 0 then -- Check if player health is 0
			local Sword = game.ServerStorage:WaitForChild("ClassicSword"):Clone() -- Added WaitForChild()
			toggle3 = false
			Sword.Parent = hit.Parent -- Put sword in character.
		end
	end
end)

Hope it helped :)

Wiki pages:

Conditional Statements

1st, whats wrong with your script is the "=", if you wanna check if something is equal to a certain value you gotta use "==". if youre having an if statement you gotta use one of these operators , they are very self-explainable but ill be glad to explain. You're script is smart for a begginer like you (if you are), but theres an easier way to check if a humanoid is dead, without check its health, just a simple event.

humanoid.Died:Connect(function()
	-- your beautiful code here
end)

I hope this helped, and i hope you understanded how to use this event! If not, glad to help

and remember, == not =

1 comment

0

link to the operators

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