Hey! Im back again, I got a bit of an issue and idk what to do. Basically to put it short; I have a monster in a little horror game I’m making; He is invisible but the only way you know he is near is when your flashlight starts flickering. I have a part within the monster model (Which is a simple dummy, you are never meant to actually see the monster (at this point in time at least)) and this part is the collision detection if you will. When this “Part” overlaps with the player it will check a few things,
A. If the player has the flashlight equipped,
B. If the flashlight is enabled (and if it is, It’ll while wait loop the brightness of the flashlight) and
C. If the flashlight is not equipped then to break the while wait loop.
C 2.0. If the player leaves the “Part” range, the flashlight will return to normal, ie brightness returns to its current value as if it wasn’t affected (3 in this case).
I have no clue where I could find reference code for this and any help is appreciated. The code below is what I have, Basically it’s a mess… It keeps on saying Backpack is index nil but im not sure what that means?
I’m still new to coding so i can guarantee rookie mistakes.
I think you should consider rewriting what you have to a simple magnitude checker.
For best optimization you’d need:
Create a function that flickers the flashlight over and over.
A function that checks if the monster is in range using magnitude between the monster and the player.
A simple check if they’re holding the flashlight, which you already have!
A never-ending loop like while true do that checks the magnitude.
In short it’d be if the player is in a certain range (magnitude), it’ll do a check to see if they’re holding it and then finally it’d start flickering. If they aren’t in magnitude it’d stop the flickering function.
It keeps on saying Backpack is index nil but im not sure what that means?
Nil usually means that it’s not visible to the Script’s eye, what type of script is it? I think you’re looking for a LocalScript, cause regular Server Scripts can’t find the Local Backpack of a player
(Normal script inside of invisible non-collidable hitbox part)
local Lights = {}
coroutine.wrap(function()
while true do
wait()
if (math.random(0,1) == 1) then
for Index, Light in pairs(Lights) do
if (math.random(0,1) == 1) then
Light.Enabled = (math.random(0,1) == 1)
end
end
end
end
end)()
local TouchConnection = script.Parent.Touched:Connect(function() end)
while true do
local TouchingParts = script.Parent:GetTouchingParts()
local FoundLights = {}
for _, Part in pairs(TouchingParts) do
for _, Descendant in pairs(Part:GetDescendants()) do
if Descendant:IsA("Light") then
table.insert(FoundLights, Descendant)
end
end
end
Lights = FoundLights
wait()
end
Sorry for my late reply, I did see this a while ago; I was looking into Magnitudes and how they worked! Thank you, you actually helped more than you think
local Lights = {}
coroutine.wrap(function()
while true do
wait()
if (math.random(0,1) == 1) then
for Index, Light in pairs(Lights) do
if (math.random(0,1) == 1) then
Light.Enabled = (math.random(0,1) == 1)
end
end
end
end
end)()
local Cache = {}
local BufferGCTime = 2.5
local LastAccessPurgeThreshold = 30
coroutine.wrap(function()
while true do
wait(BufferGCTime)
for Index, Value in pairs(Cache) do
if (tick() - Value[1]) >= LastAccessPurgeThreshold then
Cache[Index] = nil
end
end
end
end)()
local TouchConnection = script.Parent.Touched:Connect(function() end)
local MaxTouchingPartsIndex = 25
local MaxSubIndex = 10
while true do
local TouchingParts = script.Parent:GetTouchingParts()
local FoundLights = {}
for TouchingPartsIndex, Part in pairs(TouchingParts) do
if TouchingPartsIndex >= MaxTouchingPartsIndex then break end
if Cache[Part] ~= nil then
for Index, Light in pairs(Cache[Part]) do
if Index == 1 then continue end
table.insert(FoundLights, Light)
end
Cache[Part][1] = tick()
else
local Buffer = {tick()}
local SubIndexCount = 0
for _, Descendant in pairs(Part:GetDescendants()) do
if SubIndexCount >= MaxSubIndex then break else SubIndexCount += 1 end
if Descendant:IsA("Light") then
table.insert(FoundLights, Descendant)
table.insert(Buffer, Descendant)
end
end
Cache[Part] = Buffer
end
end
Lights = FoundLights
wait()
end
Its a bit laggy when it intersects alot of parts, I tried to reduce lag with this one