Howdy, today I’m working more on one of the last features of my day and night cycle but have seemingly walked into a wall I don’t know how to overcome. For some context, this feature is supposed to turn tagged NPCs visible depending on the current time of day. It also sets their collision group in order to prevent colliding with the player while they’re visible. My problem is that I haven’t been able to make it so that the NPC can also wait to reappear while the player is standing in their spot, as otherwise the player can be moved and shoved into awkward positions and potentially out of bounds. I’ve tried looking in the forums to see if someone else had ran a similar scenario in the past, but couldn’t find anything. Below are the current scripts I’m using to develop this feature. Any help will be greatly appreciated :^)
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local transitionsModule = require(ReplicatedStorage.Modules.Services.TransitionService.transitionsModule)
local npcCollisionModule = require(ReplicatedStorage.Modules.Services.NPCService.npcCollisionModule)
local lockTime = ReplicatedStorage.Values.BoolValues.lockTime
local currentTimeofDay = ReplicatedStorage.Values.StringValues.currentTimeofDay
local player = Players.LocalPlayer
local daytimeNPC = CollectionService:GetTagged("daytimeNPC")
local nighttimeNPC = CollectionService:GetTagged("nighttimeNPC")
local touched: RBXScriptConnection
--NPC Visibility
local function setNPCVisibility(npcs, visible)
for _, npc in ipairs(npcs) do
if npc:IsA("Model") then
for _, instance in ipairs(npc:GetDescendants()) do
if instance:IsA("BasePart") and instance.Name ~= "HumanoidRootPart" then
instance.Transparency = visible and 0 or 1
elseif instance:IsA("Part") and instance.Name == "HumanoidRootPart" then
instance.Transparency = 1
elseif instance:IsA("Part") then
touched = instance.Touched:Connect(function()
instance.Transparency = 1
end)
instance.Transparency = visible and 0 or 1
touched:Disconnect()
elseif instance:IsA("Decal") then
instance.Transparency = visible and 0 or 1
end
end
end
break
end
end
--Transitions
local function transitionDaytime()
if lockTime.Value == false then
player.Character.Humanoid.WalkSpeed = 0
transitionsModule.slideBlackStart()
task.wait(3)
setNPCVisibility(daytimeNPC, true)
setNPCVisibility(nighttimeNPC, false)
npcCollisionModule.setHiddenNPC(nighttimeNPC)
npcCollisionModule.setVisibleNPC(daytimeNPC)
transitionsModule.basicBlackEnd()
player.Character.Humanoid.WalkSpeed = 16
end
end
local function transitionNighttime()
if lockTime.Value == false then
player.Character.Humanoid.WalkSpeed = 0
transitionsModule.slideBlackStart()
task.wait(3)
setNPCVisibility(nighttimeNPC, true)
setNPCVisibility(daytimeNPC, false)
npcCollisionModule.setHiddenNPC(daytimeNPC)
npcCollisionModule.setVisibleNPC(nighttimeNPC)
transitionsModule.basicBlackEnd()
player.Character.Humanoid.WalkSpeed = 16
end
end
--Time of day changed
function dayNightTransitionModule.onCurrentTimeChanged()
if currentTimeofDay.Value == "Night" then
transitionNighttime()
elseif currentTimeofDay.Value == "Morning" then
transitionDaytime()
end
end
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid")
local debounce = true
local touched: RBXScriptConnection
--Collison groups
function npcCollisionModule.setHiddenNPC(npcs)
for _, npc in ipairs (npcs) do
if npc:IsA("Model") then
for _, instance in ipairs(npc:GetDescendants()) do
if instance:IsA("Part") and instance.Name == "HumanoidRootPart" then
instance.CollisionGroup = "hiddenNPC"
print("NPC is hidden")
elseif instance:IsA("BasePart") then
instance.CollisionGroup = "hiddenNPC"
end
end
end
break
end
end
function npcCollisionModule.setVisibleNPC(npcs)
for _, npc in ipairs (npcs) do
if npc:IsA("Model") then
for _, instance in ipairs(npc:GetDescendants()) do
if instance:IsA("Part") and instance.Name == "HumanoidRootPart" then
touched = instance.Touched:Connect(function()
if humanoid and debounce == true then
debounce = false
task.wait(1)
instance.CollisionGroup = "hiddenNPC"
debounce = true
end
end)
instance.CollisionGroup = "Default"
print("NPC is visible")
elseif instance:IsA("BasePart") then
touched = instance.Touched:Connect(function()
if humanoid and debounce == true then
debounce = false
task.wait(1)
instance.CollisionGroup = "hiddenNPC"
debounce = true
end
end)
instance.CollisionGroup = "Default"
end
end
end
break
end
touched:Disconnect()
end
This one started off complicated. I’m pretty sure I went in the wrong direction at first. After a second look, it seems the real issue was that NPCs appear instantly when the time changes. If a player is standing where an NPC spawns, the NPC collides with the player and pushes them. So I am checking whether a player is in the NPC’s spawn spot. If so, I wait for the player to move away before making the NPC visible.
ServerScript
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid")
local debounce = true
local touched: RBXScriptConnection
function npcCollisionModule.setHiddenNPC(npcs)
for _, npc in ipairs (npcs) do
if npc:IsA("Model") then
for _, instance in ipairs(npc:GetDescendants()) do
if instance:IsA("Part") and instance.Name == "HumanoidRootPart" then
instance.CollisionGroup = "hiddenNPC"
print("NPC is hidden")
elseif instance:IsA("BasePart") then
instance.CollisionGroup = "hiddenNPC"
end
end
end
break
end
end
function npcCollisionModule.setVisibleNPC(npcs)
for _, npc in ipairs (npcs) do
if npc:IsA("Model") then
for _, instance in ipairs(npc:GetDescendants()) do
if instance:IsA("Part") and instance.Name == "HumanoidRootPart" then
touched = instance.Touched:Connect(function()
if humanoid and debounce == true then
debounce = false
task.wait(1)
instance.CollisionGroup = "hiddenNPC"
debounce = true
end
end)
instance.CollisionGroup = "Default"
print("NPC is visible")
elseif instance:IsA("BasePart") then
touched = instance.Touched:Connect(function()
if humanoid and debounce == true then
debounce = false
task.wait(1)
instance.CollisionGroup = "hiddenNPC"
debounce = true
end
end)
instance.CollisionGroup = "Default"
end
end
end
break
end
touched:Disconnect()
end
Ya, that’s pretty hard to do without testing everything.. Anyways, that is way I also told you what I think the problem is. If you think that may be it, at least you know that script better than me..
Maybe something like this would be more of a simplistic approach.
local function canNPCSpawnHere(npc)
if not npc.PrimaryPart then return true end
local origin = npc.PrimaryPart.Position
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {workspace}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local result = workspace:Raycast(origin, Vector3.new(0, 0.1, 0), raycastParams)
-- If ray hits something, NPC can't spawn there
return result == nil
end
local function waitForSafeSpawn(npc)
while not canNPCSpawnHere(npc) do
task.wait(0.5)
end
end
Add this to your transitions. But actually, just remove the break statements first in your original script -that might fix everything. Also, in this chunk the task.wait(0.5) may be a bit too fast.
One other thing.. IF this is running right away;
local transitionsModule = require(ReplicatedStorage.Modules.Services.TransitionService.transitionsModule)
local npcCollisionModule = require(ReplicatedStorage.Modules.Services.NPCService.npcCollisionModule)
local lockTime = ReplicatedStorage.Values.BoolValues.lockTime
--------------------------------------------------------------
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local transitionsModule = require(Modules:WaitForChild("Services")
:WaitForChild("TransitionService"):WaitForChild("transitionsModule"))
local npcCollisionModule = require(Modules:WaitForChild("Services")
:WaitForChild("NPCService"):WaitForChild("npcCollisionModule"))
local lockTime = ReplicatedStorage:WaitForChild("Values")
:WaitForChild("BoolValues"):WaitForChild("lockTime")
I’m trying out the top code you pasted right now, and I do think we’re getting closer to finishing this up! The collision groups changing with the time of day at least seem to be mostly working as intended, but I believe the humanoid root part is somehow getting unanchored everytime the collision group changes. The reason why is because the NPC’s collisions are being moved (by the player?) slightly whenever the time of day changes. . .
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local transitionsModule = require(ReplicatedStorage.Modules.Services.TransitionService.transitionsModule)
local npcCollisionModule = require(ReplicatedStorage.Modules.Services.NPCService.npcCollisionModule)
local lockTime = ReplicatedStorage.Values.BoolValues.lockTime
local currentTimeofDay = ReplicatedStorage.Values.StringValues.currentTimeofDay
local player = Players.LocalPlayer
local daytimeNPC = CollectionService:GetTagged("daytimeNPC")
local nighttimeNPC = CollectionService:GetTagged("nighttimeNPC")
local function waitForSafeSpawn(npc)
local primaryPart = npc.PrimaryPart or npc:FindFirstChild("HumanoidRootPart")
if not primaryPart then return end
local checkDistance = 3
while true do
if not player.Character then break end
local playerRoot = player.Character:FindFirstChild("HumanoidRootPart")
if not playerRoot then break end
local distance = (playerRoot.Position - primaryPart.Position).Magnitude
if distance >= checkDistance then
break
end
task.wait(0.5)
end
end
local function setNPCVisibility(npcs, visible)
for _, npc in ipairs(npcs) do
if npc:IsA("Model") then
if visible then
spawn(function()
waitForSafeSpawn(npc)
for _, instance in ipairs(npc:GetDescendants()) do
if instance:IsA("BasePart") and instance.Name ~= "HumanoidRootPart" then
instance.Transparency = 0
elseif instance:IsA("Part") and instance.Name == "HumanoidRootPart" then
instance.Transparency = 1
elseif instance:IsA("Decal") then
instance.Transparency = 0
end
end
end)
else
for _, instance in ipairs(npc:GetDescendants()) do
if instance:IsA("BasePart") and instance.Name ~= "HumanoidRootPart" then
instance.Transparency = 1
elseif instance:IsA("Part") and instance.Name == "HumanoidRootPart" then
instance.Transparency = 1
elseif instance:IsA("Decal") then
instance.Transparency = 1
end
end
end
end
end
end
local function transitionDaytime()
if lockTime.Value == false then
player.Character.Humanoid.WalkSpeed = 0
transitionsModule.slideBlackStart()
task.wait(3)
setNPCVisibility(nighttimeNPC, false)
npcCollisionModule.setHiddenNPC(nighttimeNPC)
setNPCVisibility(daytimeNPC, true)
npcCollisionModule.setVisibleNPC(daytimeNPC)
transitionsModule.basicBlackEnd()
player.Character.Humanoid.WalkSpeed = 16
end
end
local function transitionNighttime()
if lockTime.Value == false then
player.Character.Humanoid.WalkSpeed = 0
transitionsModule.slideBlackStart()
task.wait(3)
setNPCVisibility(daytimeNPC, false)
npcCollisionModule.setHiddenNPC(daytimeNPC)
setNPCVisibility(nighttimeNPC, true)
npcCollisionModule.setVisibleNPC(nighttimeNPC)
transitionsModule.basicBlackEnd()
player.Character.Humanoid.WalkSpeed = 16
end
end
function dayNightTransitionModule.onCurrentTimeChanged()
if currentTimeofDay.Value == "Night" then
transitionNighttime()
elseif currentTimeofDay.Value == "Morning" then
transitionDaytime()
end
end
ModuleScript
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local npcCollisionModule = {}
local connections = {}
function npcCollisionModule.setHiddenNPC(npcs)
for _, npc in ipairs(npcs) do
if connections[npc] then
for _, connection in ipairs(connections[npc]) do
connection:Disconnect()
end
connections[npc] = nil
end
if npc:IsA("Model") then
for _, instance in ipairs(npc:GetDescendants()) do
if instance:IsA("BasePart") then
instance.Anchored = true
instance.CollisionGroup = "hiddenNPC"
end
end
end
end
end
function npcCollisionModule.setVisibleNPC(npcs)
for _, npc in ipairs(npcs) do
if npc:IsA("Model") then
local npcConnections = {}
for _, instance in ipairs(npc:GetDescendants()) do
if instance:IsA("BasePart") then
instance.Anchored = true
instance.CollisionGroup = "Default"
local connection = instance.Touched:Connect(function(hit)
if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
task.wait(1)
instance.CollisionGroup = "hiddenNPC"
end
end)
table.insert(npcConnections, connection)
end
end
connections[npc] = npcConnections
end
end
end
return npcCollisionModule
This is very fixable. It is just going to need to be hammered out. In that ocean of complicated, you just need that simple hide until they can be seen added.
Hey, sorry for the late response, I had errands to run today. Anyways, I tried out the modulescript code you sent, and with like three more lines added I was able to get it closer to functioning as intended. For the first three or so seconds the NPC is set to reappear, its collision group is set to hiddenNPC, meaning the player won’t be shoved around for that period of time. But once those few seconds are up, it goes back to moving the player around until they leave from that spot. Also in regards to the loops you’re using, was it bad practice for me to include more breaks within them? I learned that loops left running after calling a function several times gradually increases memory usage which I do know is suppose to be kept as low as realistically possible. . .
function npcCollisionModule.setVisibleNPC(npcs)
for _, npc in ipairs(npcs) do
if npc:IsA("Model") then
local npcConnections = {}
for _, instance in ipairs(npc:GetDescendants()) do
if instance:IsA("BasePart") then
instance.Anchored = true
local touched = instance.Touched:Connect(function(hit)
if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
task.wait(1)
instance.CollisionGroup = "hiddenNPC"
end
end)
local touchEnded = instance.TouchEnded:Connect(function(hit)
if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
task.wait(1)
instance.CollisionGroup = "Default"
end
end)
table.insert(npcConnections, touched)
table.insert(npcConnections, touchEnded)
end
end
connections[npc] = npcConnections
end
break
end
end
You have a break statement at the end of your outer loop that stops after processing only the first NPC. This means all other NPCs in the array won’t be processed. The logic for showing/hiding NPCs seems reversed - you’re hiding on touch and showing on touch ended.
You do understand how Break works right? .. seen this a few times now..
I thought I understood how Breaks work, don’t they just cause the loop to cease running? Or is it more complicated then how I’ve been using them? Also not unless if im somehow misunderstanding my own goals, I did intend for the npcs to be hidden on touch and showed on touch ended since im aiming for them to not just suddenly appear ontop of the player yknow?
Sound like you got it.. maybe this was just misplaced..
Looking at that line, it is looking for all the NPCs in npcs, however lower there is a break that will kick out from that first for loop after the first pass.
Sure but it will always only do one pass on that outer for loop, due to the break command.
It really looks like that just needs to be removed.. it is not making sense with the rest of the chunk.
Oh I see what the issue is now, like you said it won’t actually use the created array to loop through all NPCs. But I wonder, am I overthinking on how performance cost these loops may be? I want to be cautious of the budget while I’m already here as to make future polishing less of a headache xD
Well this would clean up the connects.. Not sure how big of a deal this would end up being.
local npcCollisionModule = {}
local connections = {}
function npcCollisionModule.setVisibleNPC(npcs)
for _, npc in ipairs(npcs) do
if npc:IsA("Model") then
local npcConnections = {}
for _, instance in ipairs(npc:GetDescendants()) do
if instance:IsA("BasePart") then
instance.Anchored = true
local touched = instance.Touched:Connect(function(hit)
if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
task.wait(1)
instance.CollisionGroup = "hiddenNPC"
end
end)
local touchEnded = instance.TouchEnded:Connect(function(hit)
if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
task.wait(1)
instance.CollisionGroup = "Default"
end
end)
table.insert(npcConnections, touched)
table.insert(npcConnections, touchEnded)
end
end
connections[npc] = npcConnections
end
end
end
return npcCollisionModule
The way I program is to just get it rolling. Super sloppy even. Then, when I’m sure I’ve got it down, I’ll go back and refine it. There’s no need to look for total refinements when it’s not even fully working.
It’s kind of a way to eliminate added problems and to make sure I’m getting valid tests.
With a case like this I would start a new program, just to test out the waiting approach.. then add that to the real program once I’m sure that works.
if either of you want my two cents; my understanding of this problem is that the poster wants their NPCs to not appear until there isn’t a player colliding with it
the way you both are trying to solve this is by using .touched to see if anything with a humanoid is colliding with an NPC, but its (probably) more reliable to just check through all player characters to see if any are within a certain distance, if so, dont appear, if not, then do appear, instead of using touched (since touched is a bit unreliable and also if there are multiple NPCs with humanoids they will interact with eachother)
also i dont fully understand what the poster wants out of this. once it turns to night (or day) do they want the NPCs to only appear if no players are touching it or to disappear when players touch them at all? (also i dont see why you wouldnt just always have the NPCs with the hiddenNPC collision group)
but also also if this the poster fully intends for them to only appear (or disappear) depending on PLAYER contact and solely player contact, then there should also be
if hit.Parent and hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) then
Hello, thanks for replying! Due to the way I currently understand collision groups, I had the NPCs switch between the default and hiddenNPC as they’re intended to be solid and collide with the player whenever they’re visible. And to be a bit more clear in regards to what I wanted, I sought for tagged NPCs to disappear and reappear with the appropriate time of day, but if there was a player standing in the same spot they are they’d wait to appear until the player has moved. Not that they’re dependent on the player’s position when disappearing. This way hopefully less instances of clipping through walls or falling over would occur from the time transitioning.
Also thanks for pointing out the need for an additional check, I’ll be adding that to the script ^ - ^
the most simple way of doing this that i can think of is just
--wait until the time of day is appropriate here
--find nearest character and its distance
--repeatedly task.wait() and check nearest character's distance until distance >= minimum satisfactory distance
--appear
and if this is handled in 1 big script that sorts through each NPC i’d just surround the code inside of the (supposed) pairs loop with a task.Spawn() so they all run separate from eachother