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

How do I make my script affect multiple players at a time?

I made a fire script that damges the player when they step on a burning part. The fire script works, but it only affects one player at a time. I have no idea how to make it apply to multiple players.

Here is the code

The first script, detects if the player is touching the part and checks of the player isn't already burning

[REGULAR SCRIPT]


script.Parent.Parent.Touched:Connect(function(SpreadPart)
	
	
	
	if SpreadPart:FindFirstChild("FireScript") == nil and SpreadPart.Locked == false then
		
		if game.Players:FindFirstChild(SpreadPart.Parent.Name) then
			if SpreadPart.Parent:FindFirstChild("Humanoid") then
				if game.Players:FindFirstChild(SpreadPart.Parent.Name) then

					local player = game.Players:FindFirstChild(SpreadPart.Parent.Name)

					if player.isBurning.Value == false then
						
						if script.Parent.Parent:FindFirstChild("Fire") then
							

							player.isBurning.Value = true
							game.Workspace.DamageSend:FireClient(player)
						end
					end
				end
			end	
		else
			script.Parent.Parent.FireScript:Clone().Parent = SpreadPart
		end
	end
end)

Then it fires the client to prepare for damge

[LOCAL SCRIPT]

local isBurning = game.Players.LocalPlayer:WaitForChild("isBurning")


game.Workspace.DamageSend.OnClientEvent:Connect(function()
	if isBurning.Value == true then
		game.Workspace.DamagePlayer:FireServer()
	end
end)

Then the local script fires another event to damage the player

[REGULAR SCRIPT]

script.Parent.OnServerEvent:Connect(function(client)
	print("Damaged:"..client.Name)
	
	local player = game.Players:FindFirstChild(tostring(client))
	local isBurning = player:FindFirstChild("isBurning")
	
	local Sound = game.ReplicatedStorage.Fire.Sound
	local Embers = game.ReplicatedStorage.Fire.Embers
	local Fire = game.ReplicatedStorage.Fire.Fire
	local Light = game.ReplicatedStorage.Fire.PointLight


	local player = game.Workspace:FindFirstChild(client.Name)

	local count = 0

	if player.UpperTorso:FindFirstChild("PointLight") then

	else
		Sound:Clone().Parent = player.UpperTorso
		Embers:Clone().Parent = player.LowerTorso
		Embers:Clone().Parent = player.UpperTorso
		Fire:Clone().Parent = player.LowerTorso
		Fire:Clone().Parent = player.UpperTorso


		Light:Clone().Parent = player.LowerTorso

		player.UpperTorso.Sound:Play()

		repeat
			if isBurning.Value == true then
				wait(0.5)

				player.Humanoid.Health = player.Humanoid.Health - 5

				count = count + 1
			else
				break
			end

		until count == 6

		player.UpperTorso.Sound:Stop()

		player.LowerTorso.Embers.Enabled = false
		player.UpperTorso.Embers.Enabled = false
		player.LowerTorso.Fire.Enabled = false
		player.UpperTorso.Fire.Enabled = false
		player.LowerTorso.PointLight:Destroy()
		
		isBurning.Value = false
		wait(1)
		player.LowerTorso.Embers:Destroy()
		player.UpperTorso.Embers:Destroy()
		player.LowerTorso.Fire:Destroy()
		player.UpperTorso.Fire:Destroy()


	end
end)

Any help is appreciated!

1 answer

The reason I think it is only affect a single player is because the last script can only focus on a single thing at a time due to the Wait() in lines 53 and line 33.

Due to the nature of things damaging over time, you can't really manage it all from a single script from my knowledge.

I suggest that you clone scripts into the player's humanoid and that script would damage the player until it is removed. That way you can have a bunch of fires acting at the same time.

To help, here is what the script would look like.

to follow: Add a part with the first script inside of it. Then, put the 2nd script in server script service. Then, put the 3rd script in replicated storage. Put a bindable event named "FireEvent" in replicated storage, then put a bindable event named "Activate" in the 3rd script.

The script belows is what is going to run when the player touches the fire brick.

local players = game:GetService("Players)
local bindable = game.ReplicatedStorage.FireEvent --This is a bindable event, this means that server scripts can access this from other server scripts. I noticed that you were using a server to client to server system which is not needed in this situation.

local part = script.Parent --Whatever part you would like here.

part.Touched:Connect(function(toucher)
	if toucher.Parent:FindFirstChild("Humanoid") then
		if player:GetPlayerFromCharacter(toucher.Parent) then
			local player= player:GetPlayerFromCharacter(toucher.Parent)
			local char = player.Character			

			if not isOnFire then--For this part, I am unsure how you check for if they are on fire, but put that here as an if then statement.
				bindable:Fire(player)
			end
		end
	end
end)

The script below is the script that detects when the bindable event is fired.

local debris = game:GetService("Debris)

local bindable = game.ReplicatedStorage.FireEvent --Whereever your fire event is.
local fireScript = game.ReplicatedStorage.fireScript

local fireDuration = 3

bindable.Event:Connect(function(player)
	local char = player.Character
	local human = char.Humanoid 

	local clone = fireScript:Clone() --Clones the script and puts it inside the humanoid
	clone.Parent = human
	clone.Activate:Fire()	

	debris:AddItem(clone,fireDuration) --VERY IMPORTANT! This is the fire duration, the script will be removed after the fire is done.
end)

The script below is the firescript that will damage the player.

local event = script:WaitForChild("Activate")

event.Event:Connect(function() -- don't worry about the while loop the debris will take care of it. 
	while true do
		human:TakeDamage(10)
		wait(1)
	end
end)

It should work now.

But, keep in mind I have not tested this. In case the code doesn't work, here is the concept behind it.

A script damaging the player gets cloned in the humanoid causing it to take damage over time. The clone will get destroyed in 3 seconds using the debris service.

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