My beam gets stuck on the floor after i die

  1. What do you want to achieve? so im using ZonePlus for this and i want my GasterBlaster/Beam to execute code while you are in a certain zone

  2. What is the issue? this is gonna sound dumb but i cant make it so when you leave the zone the Blaster stops shooting, and when i die the blaster glitches and stays in one place
    robloxapp-20221229-2244558.wmv (8.6 MB)
    code :

game:GetService(‘Players’).PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoidRootPart = character:WaitForChild(‘HumanoidRootPart’)
wait(1)
local zone = require(game.ReplicatedStorage.Zone)
local container = workspace.Container
local zone = zone.new(container)

zone.playerEntered:Connect(function(player)

		while true do
			local modelpositionpart = Instance.new('Part')
			modelpositionpart.Massless = true
			modelpositionpart.CanCollide = false
			modelpositionpart.Transparency = 1
			modelpositionpart.Parent = humanoidRootPart
			modelpositionpart.CFrame = CFrame.new(humanoidRootPart.Position.X,humanoidRootPart.Position.Y+1.5,humanoidRootPart.Position.Z-30) * CFrame.Angles(math.rad(180),0,3.15)

			local weldconstraint = Instance.new('WeldConstraint')
			weldconstraint.Parent = humanoidRootPart
			weldconstraint.Part0 = humanoidRootPart
			weldconstraint.Part1 = modelpositionpart

			repeat wait(1)
				local model = game:GetService('ServerStorage'):WaitForChild('Blaster'):Clone()
				model.Parent = workspace
				model:SetPrimaryPartCFrame(modelpositionpart.CFrame)
				wait(3.8)
				model:Destroy()
				break
			until false
		end
	end)
end)

end)

  1. What solutions have you tried so far? i tried making making a break right there as you can see but didnt work, also tried making a script where if the player dies the model instantly gets destroyed but it also didnt work, searched on google, youtube, devforum and still nothing idk what to do.

im not an expert at scripting, im still learning but i really need help with this one cuz im making an obby/survival game and this is fundamental for it.

btw does this script work with what im trying to do or should i erase it and try another way?

Okay so I don’t use ZonePlus, but I use a custom position check.

The way I run the checks is as following:

I create a table of players, with a true/false value. You can also simply use an attribute on the character.

Then run the check function (To see if they are inside the zone)

If they are, and the attribute/table entry is false, switch it to true. When firing, check if this attribute is true.

Then if they aren’t in the zone, and it’s true, change it to false.

This should handle most of your issues with any debounce. Don’t handle everything in one function, break it up.

As for your blaster glitching, when the player dies go through and delete it.

Not entirely sure what you’re expecting. A video or a picture of the issue would be helpful.
-Edit- just saw the video - my bad.

But for the immediate issue in this script - you have an infinite while loop that is triggered by an event that theoretically can be triggered an infinite amount of times, which means infinite while loops.

The second issue I see if that your variable humanoidRootPart can be associated with a completely different player.

For example;

  1. Player 1 joins the game - humanoidRootPart = player1.Character.RootPart
  2. Player 2 joins the game - humanoidRootPart = player2.Character.RootPart
  3. Player 1 triggers zone enter event - humanoidRootPart is still referencing Player 2.

To do:

  1. Move your humanoidRootPart definition into the playerEntered event so you’re only dealing with that player’s stuff.
  2. Remove the while or add a break condition
  3. Avoid using loops (while, repeat) in events, unless there’s a specific use case you need, in which case use a Debounce.

What is a debounce?

local debounce = false; -- when true - prevent code actioning

zone.playerEntered:Connect(function(player)
	if (debounce == true) return end -- stop the following code from actioning if the event has already been called.

	wait(5)
	debounce = false -- you can now call the event again.
end)

-Edit-
Just seeing the video this issue is occurring because of your while loop not having an exit condition.

If the zones system you’re using has a ‘playerLeft’ event - use that to detect when the player has left to flip a boolean.


local inZone = false;
zones.playerLeft:Connect(function(player)
	inZone = false
end)
zones.playerEntered:Connect(function(player)
	if (inZone) then return end -- don't want to trigger this multiple times.
	inZone = true;
	
	while (inZone) do
		-- etc.
	end
end)

If you need to keep track of multiple players that can enter the zone - then you can adjust the code like so:

local playersInZone = {}
zones.playerLeft:Connect(function(player)
	table.remove(playersInZone,player)
end)
zones.playerEntered:Connect(function(player)
	if (table.find(playersInZone, player)) then return end -- don't trigger multiple events

	table.insert(playersInZone,player)

	while (table.find(playersInZone,player)) do
		--etc.
	end
end)

Hello, sorry for the late reply, so i made it so when i leave the zone the blaster stops shooting, great but i didnt quite understand the rest when u said something about tables and stuff, well it doesnt matter too much since i want to make my game single player but i still have this issue of the blaster stuck in one place
robloxapp-20221230-2124129.wmv (3.9 MB)
and i deleted the repeat loop since that made the blaster keep shooting after i left the zone
but now the blaster just spawns like in front of the zone and not infront of the humanoid root part, check the video youll see what i mean.

updated code :

game:GetService(‘Players’).PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoidRootPart = character:WaitForChild(‘HumanoidRootPart’)

	local debounce = false
	
	local zone = require(game.ReplicatedStorage.Zone)
	local container = workspace.Container
	local zone = zone.new(container)

local inZone = false
zone.playerExited:Connect(function(player)
inZone = false
end)

zone.playerEntered:Connect(function(player)
if (inZone) then
return
end
inZone = true

while (inZone) do
	local modelpositionpart = Instance.new('Part')
				modelpositionpart.Massless = true
				modelpositionpart.CanCollide = false
				modelpositionpart.Transparency = 1
				modelpositionpart.Parent = humanoidRootPart
				modelpositionpart.CFrame = CFrame.new(humanoidRootPart.Position.X,humanoidRootPart.Position.Y+1.5,humanoidRootPart.Position.Z-30) * CFrame.Angles(math.rad(180),0,3.15)

				local weldconstraint = Instance.new('WeldConstraint')
				weldconstraint.Parent = humanoidRootPart
				weldconstraint.Part0 = humanoidRootPart
				weldconstraint.Part1 = modelpositionpart


					local model = game:GetService('ServerStorage'):WaitForChild('Blaster'):Clone()
					model.Parent = workspace
					model:SetPrimaryPartCFrame(modelpositionpart.CFrame)
					wait(3.8)
					model:Destroy()
				if (debounce == true)  then 
					return end

				wait(2)
				debounce = false
		end	
	end)
end)

end)