Script stopped working

Hello! This used to work on RunContext = client but now it doesn’t work. All the sudden, it has stopped working. It only works when I put it in legacy. I didn’t touch anything.

-- RunContext = Client

local Folder = script.Parent:GetDescendants()
local DB = true

for i, v in pairs(Folder) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			local hum = hit.Parent:FindFirstChild("Humanoid")
			
			if hum then
				if DB then
					DB = false
					hum.Health = 0
					
					task.wait(1)
					
					DB = true
				end
			end
			
		end)
	end
	
end
4 Likes

Could you give more info? Like the script’s location?

1 Like

It’s under a folder in workspace.

1 Like

Is it a LocalScript or is it a “normal Script”.

I would presume it’s a “normal Script” because you cant change the run context with a LocalScript

Still gonna ask though

1 Like

Normal script with RunContext set to Client. It’s just so weird, all the sudden, it simply stopped working

1 Like

If it helps, I have a picture here. “Hit” is the script that stopped working.

context

1 Like

I don’t think .Touched events works on the client, even if the run context is set to client. I’ve tried using it multiple times, but it just won’t work the way it’s supposed to. It’s a weird situation, but you could try this instead:

local Folder = script.Parent:GetDescendants()
local DB = true

function GetHumanoid(obj: BasePart)
	local Params = OverlapParams.new() -- This isn't required, but I recommend you use it for at least some slightly optimized code.
	Params.MaxParts = 100 -- How many instances the script can detect at once.
	Params.FilterDescendantsInstances = {obj} -- What instances the script should ignore.
	
	while true do
		local PartsInPart = workspace:GetPartsInPart(obj, Params) -- Gets whatever parts are making contact with "obj", except for ignored parts.
		
		for i, v in PartsInPart do -- Loop that goes through all of the parts making contact with "obj"
			local hum = v.Parent:FindFirstChildOfClass("Humanoid") -- Script attempts to find a "Humanoid" instance.
			
			if hum and DB == true then -- If the script finds the humanoid and the debounce is true, then...
				DB = false
				hum.Health = 0
				
				task.wait(1) -- Waiting for one second.
				
				DB = true
			elseif not hum then -- If the script can't find the humanoid, then...
				Params:AddToFilter(v) -- Adds the instance to the filter so that the script will ignore it from now on.
			end
		end
		task.wait() -- Waits for one frame.
	end
end

for i, v in pairs(Folder) do
	if v:IsA("BasePart") then
		task.spawn(GetHumanoid, v) -- Calls the function without delaying the code. Removing this would result in only one part receiving the script's attention.
	end
end

If you have any complications still, let me know.

5 Likes

Hello! Thank you for responding. The code didn’t work for and didn’t kill the player.

1 Like

I see. I will look further into this issue. Thanks for your patience.

2 Likes

Let me rephrase that better, it does work only if it’s a legacy script, but I want it client sided. Touched event worked for the time being when it was a client RunContext but then it just didn’t.

1 Like

Thanks for the information. I’m still looking into the issue. My code seems to stop running at the for loop, which is rather peculiar.

1 Like

I had the same problem! After the loop, it just didn’t work.

1 Like

maybe try adding a while loop before the for loop? I’m not sure but I’m pretty sure for i, v loops only run once

1 Like

I am pretty sure i, v loops doesn’t run once as my other projects uses this loop, and they run infinitely until told otherwise. Thank you for the reply.

1 Like

The script works. Only ingame, in Roblox studio, it doesn’t work but if I play test in my iPad or phone, it works. I’ve updated it, and it works. Your script works, and mines too, perhaps, roblox studio has a bug where it doesn’t work there.

@Wizzthepixelcat

1 Like

I figured out what the problem is. The problem is that the LocalScript is still loading these parts, which is why the for loop doesn’t work. In this your case, the for loop method that you are using only runs once.

Since the script runs really fast but the client at the same time is having trouble loading the parts, the parts are never found, and therefore, the script stops at the for loop because it didn’t find any instances except the script itself. I’ll try to find a workaround for this, but I can’t guarantee much.

2 Likes

I see. I’ve retest everything again, and some servers works, but some doesn’t work which just depends on luck. Thank you for your help!

Edit: To add on, some parts kills you, and most don’t kill you. This is very weird as just days before, everything worked just fine.

1 Like

Try this.

local ContentProvider = game:GetService("ContentProvider")

repeat task.wait() until game:IsLoaded()
print("Game loaded!")

local Assets = workspace:GetDescendants()
local MaxAssets = #Assets

for i, loadingAsset in Assets do
	ContentProvider:PreloadAsync({loadingAsset})
end

local Folder = script.Parent
local Descendants = Folder:GetDescendants()
local DB = true

function GetHumanoid(Obj: BasePart)
	local Connection = nil
	
	local Params = OverlapParams.new()
	Params.MaxParts = 100
	Params.FilterDescendantsInstances = {Obj}
	
	while true do
		if DB == true then
			local PartsInPart = workspace:GetPartsInPart(Obj, Params)

			for i, v in PartsInPart do
				if v:FindFirstAncestorOfClass("Model") then
					local Character = v.Parent
					local Humanoid = Character:FindFirstChildOfClass("Humanoid")

					if Humanoid and Humanoid.Health > 0 then
						DB = false
						Humanoid.Health = 0
						
						task.wait(1)
						
						DB = true
					end
				end
			end
		end
		task.wait()
	end
end

for i, v in Descendants do
	if v:IsA("BasePart") then
		print("BasePart exists.")
		task.spawn(GetHumanoid, v)
	end
end
2 Likes

I am sorry, it didn’t work. Nor print anything.

Edit: It printed “Game Loaded!”

1 Like

Really??? I tested it and it worked for me. Very weird. I might have to convert this to a RemoteEvent.

1 Like