Script not working in-game

I made a local script that creates new functions for a set of areas every time the player enters the game, and his character is loaded. In the studio it works perfectly, but not in the game.

Local Script:


if not game:IsLoaded() then
	game.Loaded:Wait()
end

local PlayerService = game:GetService('Players')
local RP = game:GetService('ReplicatedStorage')
local UIS = game:GetService('UserInputService')

local Module = require(RP:WaitForChild('Modules')["SoundAreaDevice.v2"])
local MusicFolder = workspace:WaitForChild('MusicAreas')

local Player = PlayerService.LocalPlayer

Player.CharacterAdded:Connect(function(Character)
	for _, Areas in MusicFolder:GetChildren() do
		local Area = Module.New(Player,Character,Areas)
	end
	warn('Operating')
	
end)

Module Script:

local RunService = game:GetService('RunService')
local TweenService = game:GetService('TweenService')

local AreaBehavior = {}
AreaBehavior.__index = AreaBehavior

function AreaBehavior.New(Player,Character,Area)
	local self = setmetatable({},AreaBehavior)

	coroutine.wrap(function()
	self.Player = Player
	self.Character = Character
	self.Area = Area
	self.Attachments = {}
	for _, Attachment in Area:GetChildren() do
		if Attachment:IsA('Attachment') then
			table.insert(self.Attachments,Attachment)
		end
	end
	self.Functions = {}
	self.Trash = {}

	self:Start()
	end)()

	return self
end

function AreaBehavior:Start()

	local Player = self.Player
	local Character = self.Character
	local Area = self.Area
	local AttachmentsTable = self.Attachments
	local Actions = self.Functions
	local Trash = self.Trash

	Actions.PlayerAdd = function()
		print(Area.Name,'running')
		while RunService.RenderStepped:Wait() do
			local OverLamp = OverlapParams.new()
			OverLamp.FilterType = Enum.RaycastFilterType.Include
			OverLamp.FilterDescendantsInstances = {Character}
			local ObjectsInsideArea = workspace:GetPartsInPart(Area,OverLamp)

			if #ObjectsInsideArea > 0 then
				if not Character:GetAttribute('InsideZone') then
					Character:SetAttribute('InsideZone',Area.Name)
					print(Character.Name,'is inside zone',Area.Name)

					local PlayerVolume = Player:GetAttribute('Volume')
					local MusicPlaying = Area:GetAttribute('Playing')
					local Creator = Area:GetAttribute('Creator')
					local AlbumDecal = Area:GetAttribute('AlbumDecal')

					for _, Attachment in AttachmentsTable do
						for _, Music in Attachment:GetChildren() do
							if Music:IsA('Sound') then
								TweenService:Create(Music,TweenInfo.new(.4),{Volume = PlayerVolume}):Play()
							end
						end
					end
				end
			else
				if Character:GetAttribute('InsideZone') == Area.Name then
					Character:SetAttribute('InsideZone',nil)
					print(Character.Name,'is out zone',Area.Name)

					for _, Attachment in AttachmentsTable do
						for _, Music in Attachment:GetChildren() do
							if Music:IsA('Sound') then
								TweenService:Create(Music,TweenInfo.new(.4),{Volume = 0}):Play()
							end
						end
					end
				end
			end
		end
	end

	Actions.PlayerDied = Character.Humanoid.Died:Connect(function()
		table.clear(self)
	end)

	Actions.PlayerAdd()

end

return AreaBehavior
1 Like

Video of both


it’s probably from your local script. it could be that the character model loads faster than the player.characteradded you have. go ahead and try to change it to something else that’s more reliable

As you can see in the video the function is firing when warn “operating”

oh i see, then it might be that the local script runs faster than the children that are loaded in your musicfolder.

basically,
local script played → models are loaded in

when it should be
models are loaded in → local script played

if the local script is loaded faster than the model, then the loop won’t pick up anything.

sorry fixed mistake

1 Like

Local Script:

if not game:IsLoaded() then
	game.Loaded:Wait()
end

local PlayerService = game:GetService('Players')
local RP = game:GetService('ReplicatedStorage')
local UIS = game:GetService('UserInputService')

local Module = require(RP:WaitForChild('Modules')["SoundAreaDevice.v2"])
local MusicFolder = workspace:WaitForChild('MusicAreas')

local Player = PlayerService.LocalPlayer

local function setupAreasForCharacter(Character)
	for _, Areas in MusicFolder:GetChildren() do
		local Area = Module.New(Player, Character, Areas)
	end
	warn('Operating')
end

Player.CharacterAdded:Connect(function(Character)
	setupAreasForCharacter(Character)
end)

-- Call the function immediately if the character already exists
if Player.Character then
	setupAreasForCharacter(Player.Character)
end

Module Script:

local RunService = game:GetService('RunService')
local TweenService = game:GetService('TweenService')

local AreaBehavior = {}
AreaBehavior.__index = AreaBehavior

function AreaBehavior.New(Player, Character, Area)
	local self = setmetatable({}, AreaBehavior)

	coroutine.wrap(function()
		self.Player = Player
		self.Character = Character
		self.Area = Area
		self.Attachments = {}
		for _, Attachment in Area:GetChildren() do
			if Attachment:IsA('Attachment') then
				table.insert(self.Attachments, Attachment)
			end
		end
		self.Functions = {}
		self.Trash = {}

		self:Start()
	end)()

	return self
end

function AreaBehavior:Start()
	local Player = self.Player
	local Character = self.Character
	local Area = self.Area
	local AttachmentsTable = self.Attachments
	local Actions = self.Functions
	local Trash = self.Trash

	Actions.PlayerAdd = function()
		print(Area.Name, 'running')
		while RunService.RenderStepped:Wait() do
			local OverLamp = OverlapParams.new()
			OverLamp.FilterType = Enum.RaycastFilterType.Include
			OverLamp.FilterDescendantsInstances = {Character}
			local ObjectsInsideArea = workspace:GetPartsInPart(Area, OverLamp)

			if #ObjectsInsideArea > 0 then
				if not Character:GetAttribute('InsideZone') then
					Character:SetAttribute('InsideZone', Area.Name)
					print(Character.Name, 'is inside zone', Area.Name)

					local PlayerVolume = Player:GetAttribute('Volume')
					local MusicPlaying = Area:GetAttribute('Playing')
					local Creator = Area:GetAttribute('Creator')
					local AlbumDecal = Area:GetAttribute('AlbumDecal')

					for _, Attachment in AttachmentsTable do
						for _, Music in Attachment:GetChildren() do
							if Music:IsA('Sound') then
								TweenService:Create(Music, TweenInfo.new(.4), {Volume = PlayerVolume}):Play()
							end
						end
					end
				end
			else
				if Character:GetAttribute('InsideZone') == Area.Name then
					Character:SetAttribute('InsideZone', nil)
					print(Character.Name, 'is out zone', Area.Name)

					for _, Attachment in AttachmentsTable do
						for _, Music in Attachment:GetChildren() do
							if Music:IsA('Sound') then
								TweenService:Create(Music, TweenInfo.new(.4), {Volume = 0}):Play()
							end
						end
					end
				end
			end
		end
	end

	Actions.PlayerDied = Character:WaitForChild("Humanoid").Died:Connect(function()
		table.clear(self)
	end)

	Actions.PlayerAdd()
end

return AreaBehavior

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.