-
What do you want to achieve? My AI runs on a single client handler, which handles all AI present in collection service.
-
What is the issue? The client handler is supposed to insert sounds and set the ids to them but it only works sometimes, not for every AI Tagged in the collection service.
-
What solutions have you tried so far? I tried to fix it myself, but it doesn’t work
local ts:TweenService = game:GetService("TweenService")
local cs:CollectionService = game:GetService("CollectionService")
local rs:ReplicatedStorage = game:GetService("ReplicatedStorage")
local cc = workspace.CurrentCamera
local make_sound = function(id:string, name:string):()
local prefix:string = "rbxassetid://"
local s:Sound = Instance.new("Sound", script)
s.Name = name
s.SoundId = prefix..id
return s
end
local get_conf = function(ai:Model, actual:boolean):()
local a = actual == false and (require(ai:FindFirstChild("Configuration"))) or ai:FindFirstChild("Configuration")
return a
end
local chase_event = function(ai:Model):()
local mod = get_conf(ai, false)
local fol:Folder = rs[ai.Name]
local found:RemoteEvent = fol:FindFirstChild("Found")
local chase_theme:Sound = make_sound(mod.SOUNDS.CHASE, "Chase "..ai.Name)
found.OnClientEvent:Connect(function(a:boolean, s:string)
if a then
ts:Create(cc, TweenInfo.new(3), {FieldOfView = 110}):Play()
if not chase_theme.Playing then
chase_theme:Play()
end
else
ts:Create(cc, TweenInfo.new(3), {FieldOfView = 70}):Play()
if chase_theme.Playing then
chase_theme:Stop()
end
end
end)
end
local jumpscare = function(ai:Model):()
local mod = get_conf(ai, false)
local fol:Folder = rs[ai.Name]
local jumpscare_event:RemoteEvent = fol:FindFirstChild("Jumpscare")
local jumpscare_sound:Sound = make_sound(mod.SOUNDS.JUMPSCARE, "Jumpscare "..ai.Name)
local jumpscare_part = ai:FindFirstChild("CameraPart")
jumpscare_event.OnClientEvent:Connect(function(js_dur:number, after_js:number)
cc.CameraType = Enum.CameraType.Scriptable
local t = ts:Create(cc, TweenInfo.new(.3), {CFrame = jumpscare_part.CFrame})
t:Play()
t.Completed:Wait()
jumpscare_sound:Play()
local jumpscare_connection:RBXScriptConnection = game:GetService("RunService").RenderStepped:Connect(function()
cc.CFrame = jumpscare_part.CFrame * CFrame.new(math.random(-2,2)/100, math.random(-2,2)/100, math.random(-2,2)/100)*CFrame.Angles(math.rad(math.random(-2,2)/10),math.rad(math.random(-2,2)/10),math.rad(math.random(-2,2)/10))
end)
wait(js_dur)
jumpscare_connection:Disconnect()
local sg = Instance.new("ScreenGui", game.Players.LocalPlayer.PlayerGui)
local fr = Instance.new("Frame", sg)
sg.IgnoreGuiInset = true
fr.BackgroundColor3 = Color3.new(0, 0, 0)
fr.Size = UDim2.new(1,0,1,0)
wait(after_js)
cc.CameraType = Enum.CameraType.Custom
cc.CameraSubject = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
end)
end
local heartbeat = function(ai:Model):()
local mod = get_conf(ai, false)
local plr = game.Players.LocalPlayer
if mod.EXTRA.HEARTBEAT then
script.hb:Play()
script.hb.PlaybackSpeed = math.clamp((ai:FindFirstChild("HumanoidRootPart").Position - plr.Character:FindFirstChild("HumanoidRootPart").Position).Magnitude, .8, 1.2)
else
if script.hb.IsPlaying then
script.hb:Stop()
end
end
end
local spwn = function(func, ...):()
local c = coroutine.wrap(func)
c(...)
end
local ai_main = function(ai:Model):()
chase_event(ai)
jumpscare(ai)
heartbeat(ai)
end
for i:number, v:Model in ipairs(cs:GetTagged("AI")) do
spwn(ai_main, v)
end
Please help!