May someone, please, tell me what's wrong with my flamethrower script?

I am wanting to make a flamethrower, and I have a script to make 3 parts used as the flame parts do damage. No matter how many attempts, it keeps on not doing damage on humanoids. I have done different types of systems in the function, and still nothing. Here’s my latest attempt:

-- 
local FlameThrower = script.Parent
local lowbar = FlameThrower.FireLowBar
local midbar = FlameThrower.FireMiddleBar
local highbar = FlameThrower.FireHighBar
local Players = game:GetService("Players")

local function OnTouched(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local humanoid = player:FindFirstChild("Humanoid")
		if humanoid then
			humanoid:TakeDamage(0)
			if humanoid and FlameThrower.Activated then
				humanoid:TakeDamage(10)
			end
		end
	end
end

lowbar.Touched:Connect(OnTouched)
midbar.Touched:Connect(OnTouched)
highbar.Touched:Connect(OnTouched)

If you cannot find a solution, thank you for your time still! :grin:
robloxapp-20240914-1333594.wmv (1.2 MB)

local player = Players:GetPlayerFromCharacter(hit.Parent)

^ This is referencing the Player object, not the Character itself.

local humanoid = player:FindFirstChild("Humanoid")

^ This will NOT detect the Character’s Humanoid.

Instead, you need to reference:

local humanoid = player.Character:FindFirstChild("Humanoid") -- .Character will set a path to the players' character!

Hope this helps.

So basically, do I replace the player term in the

local player = Players:GetPlayerFromCharacter(hit.Parent)

verse with character? I’m still learning so I’m just keeping note.

That piece of code will reference the Player for you, you want to keep that.

If you put a .Character next to your player variable, then it will reference the Character.

-- MODIFIED FUNCTION
local function OnTouched(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local humanoid = player.Character:FindFirstChild("Humanoid") -- .Character will reference your character
		if humanoid then
			humanoid:TakeDamage(0)
			if humanoid and FlameThrower.Activated then
				humanoid:TakeDamage(10)
			end
		end
	end
end

If I didn’t answer your question coherently, please let me know. I’m unsure what you meant by “verse.”

It still didn’t do damage on Humanoids (NPCs for example). Is there any other factors I need to mention that might be affecting its success?

local function OnTouched(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local humanoid = player.Character:FindFirstChild("Humanoid")
		if humanoid then
			if FlameThrower.Activated then
				humanoid:TakeDamage(10)
			end
		end
	end
end

Try this instead.
I don’t know why you had Humanoid:TakeDamage(0), but this function might fix your issue.

My logic was that if it recognizes a humanoid, it wouldn’t do damage (0) until activated, or left clicked.
(10)
I’m still a little new, so I might have made a faulty attempt. Basically, I tried having the touched system and Activated system work alongside each other.

I managed to figure out an idea that works. But despite that, since you at least were willing to help, I gave your post the solution for your troubles. Have a great day! :+1:t2:
-BreeZ

P.S. if you wanna try it out for yourself, the game is Bloxity City!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.