How do i make a script fire multiple times seperately?

So im making a game similar to ability wars or slap battles, and currently am making an ability called “peel”
peel will be able to place multiple banana peels, and when someone steps on them, they get destroyed and ragdoll the player. im having an issue with destroying the peel. when you place multiple, all of them are destroyed even when only one is stepped on. how do I fix this?

script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr)
	if cooldowns.is_cd(plr, attack_name) or db == true or main.get_data(plr.Character)['canattack'] == false or main.get_data(plr.Character)['stunned'] == true then return end
	db = true
	spawn(function()
		task.wait(dbtime)
		db = false
	end)
	
	cooldowns.set_cd(plr, attack_name, cooldown)
	
	table.clear(damaged)
	stop = false
	
	local char = plr.Character
	local hrp = char.HumanoidRootPart
	local hum = char.Humanoid
	local animator = hum.Animator
	
	local anim = animator:LoadAnimation(script.Parent.Parent.anims.use)
	
	repeat wait() until anim.Length > 0
	
	local acc = script.Parent.Parent.assets.peel_accessory:Clone()
	acc.Parent = char
	acc.Motor6D.Part0 = char['Right Arm']
	acc.Motor6D.Part1 = acc
	
	game.Debris:AddItem(acc, anim.Length)
	
	anim:Play()
	
	local windupt = tick()
	
	game.ServerScriptService.game.events.stunned.Event:Connect(function(target)
		if target == char and tick() - windupt <= windup then
			stop = true
			anim:Stop()
			acc:Destroy()
			return
		end
	end)
	
	task.wait(windup)
	
	hum.Health += heal
	
	local touched = false

	task.spawn(function()
		local peel = script.Parent.Parent.assets.peel:Clone()
		peel.CFrame = char['Right Arm'].CFrame
		peel.Anchored = false
		peel.Parent = workspace
		peel.CollisionGroup = 'antiplayer'

		game.Debris:AddItem(peel, duration)

		local bv = Instance.new('BodyVelocity', peel)
		bv.MaxForce = Vector3.new(math.huge, 0, math.huge)
		bv.Velocity = hrp.CFrame.LookVector * -25
		game.Debris:AddItem(bv, 0.1)

		local ignore_names = {'peel', 'peel_accessory'}

		peel.Touched:Connect(function(hit)
			if touched == false then
				local can_continue = true

				if table.find(ignore_names, hit.Name) or hit.Parent == char then
					can_continue = false
				end

				if can_continue == true then
					touched = true
					ts:Create(peel, TweenInfo.new(0.5), {Transparency = transparency}):Play()

					local t = tick()
					local loop;

					loop = game["Run Service"].Heartbeat:Connect(function()
						if tick() - t > duration or #damaged > 0 then
							loop:Disconnect()
							peel:Destroy()
							return
						else
							for _, v_char in main.run_hitbox('radius', peel.CFrame, peel.Size.X / 2) do
								if table.find(damaged, v_char) then else
									table.insert(damaged, v_char)

									main.hit_reg(v_char, char)

									main.damage(v_char, char, damage)
									main.stun(v_char, ragdolltime + 0.1)

									main.knockback(v_char, v_char.HumanoidRootPart.CFrame.LookVector * -1, 15, 30, 0.05, ragdolltime)
								end
							end
						end
					end)
				end
			end
		end)
	end)
end)

note:
main.run_hitbox works through workspace:GetPartsBoundInBox
the script fires when a remoteevent is fired, which happens when the player presses the ‘e’ key
sorry for the messy code i made this in like 20 mins

2 Likes

The issue could be something with how your sharing the touched variable for all peels since when any peel is touched the variable becomes true and every other connected sees that and destroys it all at once
So what I would recommend is to change this part:

local touched = false

task.spawn(function()
	local peel = script.Parent.Parent.assets.peel:Clone()
	-- ...
	peel.Touched:Connect(function(hit)
		if touched == false then
			-- ...
		end
	end)
end)

Into this part:

task.spawn(function()
	local peel = script.Parent.Parent.assets.peel:Clone()
	local touched = false -- moved here so each peel gets its own touched variable

	-- ...
	peel.Touched:Connect(function(hit)
		if touched == false then
			-- ...
		end
	end)
end)

I hope this helps.

it didnt work but thanks anyways

ok so lowk im an idiot i made it so the peel destroys itself when the damaged table contains more than 1 person, but the damaged table didnt duplicate do when one person hit the banana the other peels thought they got hit too :sob:

1 Like

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