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
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
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.
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.
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.
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.
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