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

<Answered> Can anyone explain to me why my fire script doesn't apply damage to the player?

Hello! I've been trying to create a script that sets the player on fire, and causes them to take damage over time. While the effect of the fire is there, the player doesn't take any damage for some reason. This is the script

local firePart = script.Parent

firePart.Transparency = 1

local function LightOnFire(part)

local fire = part:FindFirstChild("Fire")

if not fire then

fire= Instance.new("Fire")

fire.Parent = part

while true do

game.Players.LocalPlayer.Character.Humanoid.Health =game.Players.LocalPlayer.Character.Humanoid.Health - 15

wait(5)

end

end

end

firePart.Touched:connect(LightOnFire)

-- what I want to do is add in a part to the script where if the player

--steps on the fire, they take damage

1 answer

✓ Accepted answer

LocalPlayer is nil to the server

You are using Players.LocalPlayer in the server side. This is going to be nil. The server is not a player, and has no player of its own either.

You can get the player from their character via the Players:GetPlayerFromCharacter method.

local firePart = script.Parent;
local Players = game:GetService("Players");

firePart.Touched:Connect(function(part)
	local client = Players:GetPlayerFromCharacter(part.Parent);

	if (client) then
		--// Light them on fire! client is now the player. You can access their character using client.Character
		
	end
end);

3 comments

0

ahh This looks nice, but is the semi colon at the end of each line required? I haven't seen anyone use them before.

0

And as for the function(part). Could I insert my own function to replace part?

0

and lastly, how would I get the player to take damage in this sense? Like how would I be able to access their health?

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