Functions behavior opposite of what is expected, don't know why

This is a code block from a local script, the function’s task is to randomly select a specific delivery point and either activate it or not. For some reason, it doesn’t do that, it gets to the first item in the specified model and breaks at the line where it sees if the indexed object is a base part or not.

Here is the code block:

if Message == "RouteGen" then
		local BoxesDone = 0
		CurrentRoute = Value1
		------------------------------------------------------------
		for index, address in pairs(Value1:GetDescendants()) do
			local CoinFlip = math.random(2)
			if address:IsA("BasePart") then
				if address.ProximityPrompt.ActionText == "Deliver Package" then
					if BoxesDone < Value2 then
						address.ProximityPrompt.Enabled = true
						address.BillboardGui.Enabled = true
						DeliveryQuota += 1
						BoxesDone += 1
					end
				elseif address.ProximityPrompt.ActionText == "Deliver Mail" then
					if CoinFlip == 1 then
						address.ProximityPrompt.Enabled = true
						address.BillboardGui.Enabled = true
						DeliveryQuota += 1
					end
				end
			end
		end
	end

Here is the full script for anyone wanting to see it all.

-- Mapleridge County Mail System (Client)
--- Created: 11/26/2023
---- Original Programer: Sterling_ODeaghaidh(10283957)
-----------------------Globals------------------------

Event                         = game.ReplicatedStorage.MailEvent
LocalPlayer                   = game.Players.LocalPlayer

----------------------Variables-----------------------

TickTock                      = 0
Animation                     = nil
CurrentAnimationId            = 0
NameCount                     = 0
Names                         = {"John Reece","Zade Winter","Carol Baskin","Jacob Withers","Donncha Dean","Yeter Keo","Alekto Oliveira","Conn Tanner","Emilia Akai",
	"Winter O hAonghuis","Nosizwe Dean","Willemina Ciernik","Priapos Huber","Alfthryth Shapiro","Artan Borisov","Lojze Schüttmann","Leighton Marszalek","Kamiyah Schmitt",
	"Raginald Tsunoda","Bart De Haan","Jormungandr Bonner","Evonne O'Leary","Ognjan Lippi","Claudio Wang","Adam Marmo","Berenice Murphy","Verena Escamilla","Rachelle Moriyama",
	"Bahati Bermudez","Generoso Crocetti","Chukwuemeka Brennan","Gianna Donaghue","Marius Yamasaki","Yarik Morris","Sterling Dafydd","Ascension MacCarrick","Carl Fredrickson",
	"Emilia Maekawa","Gheorghe Sarno","Fermin Pellegrini","Emmanuele Valero","Kyou O Fionnagain","Iunia Ruiz"
}
RandomSpawn                   = false
SpawnOccupied                 = false
CurrentRoute                  = nil
DeliveryQuota                 = 0
RouteProgress                 = 0 
  
----------------------Main Code-----------------------

-- Tallying Names
for index, Name in ipairs(Names) do
	NameCount += 1
end

-- Gives the carrying animation.
function playanim(Id)
	
	local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")
	local CurrentAnimationId = "rbxassetid://"..tostring(Id)
	if Character~=nil and Humanoid~=nil then
		local animation = Instance.new("Animation",Character)
		animation.Name = "LocalAnimation"
		animation.AnimationId = CurrentAnimationId
		Animation = Humanoid:LoadAnimation(animation)
		Animation:Play()
	end
	
end


-- Remote Event
Event.OnClientEvent:Connect(function(Message,Value1,Value2,Value3,Value4)
	local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")
	----
	if Message == "Tote" then
		if Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
			playanim(15468513430)
		else
			playanim(15468490135)
		end
	end
	----
	if Message == "TakeTote" then
		local Animatior = Humanoid:FindFirstChildOfClass("Animator")
		if Animatior then
			Animatior:Destroy()
			wait(.1)
			local P = Instance.new("Animator")
			P.Name = "Animator"
			P.Parent = Humanoid
		end
	end
	----
	if Message == "Delivered" then
		RouteProgress += 1
		Value1.ProximityPrompt.Enabled = false
		Value1.BillboardGui.Enabled = false
		if RouteProgress == DeliveryQuota then
			RouteProgress = 0
			Event:FireServer("Destroy Me",CurrentRoute)
		end
	end
	----
	if Message == "Ready Office" then
		for i, c in pairs(Value1:GetChildren()) do
			if c.Name == "Box" then
				local Num = math.random(NameCount)
				c.Name = Names[Num]
				c.ProximityPrompt.ObjectText = Names[Num]
				c.ProximityPrompt.Enabled = true
			end
		end 
	end
	----
	if Message == "NewSpawn" then
		if Value1.Name == "MaplerdgeSpawn" then
			RandomSpawn = game.Workspace.MTPoint
		elseif Value1.Name == "PrinceSpawn" then
			RandomSpawn = game.Workspace.PlPoint
		elseif Value1.Name == "WainrightSpawn" then
			RandomSpawn = game.Workspace.CFBPoint
		else
			RandomSpawn = false
		end
		Event:FireServer("New NPC",RandomSpawn)
	end
	----
	if Message == "Deleted" then
		SpawnOccupied = false
	end
	----
	if Message == "RouteGen" then
		local BoxesDone = 0
		CurrentRoute = Value1
		------------------------------------------------------------
		for index, address in pairs(Value1:GetDescendants()) do
			local CoinFlip = math.random(2)
			if address:IsA("BasePart") then
				if address.ProximityPrompt.ActionText == "Deliver Package" then
					if BoxesDone < Value2 then
						address.ProximityPrompt.Enabled = true
						address.BillboardGui.Enabled = true
						DeliveryQuota += 1
						BoxesDone += 1
					end
				elseif address.ProximityPrompt.ActionText == "Deliver Mail" then
					if CoinFlip == 1 then
						address.ProximityPrompt.Enabled = true
						address.BillboardGui.Enabled = true
						DeliveryQuota += 1
					end
				end
			end
		end
	end

end)

-- loop
while true do wait(1)
	
	TickTock += 1
	----
	if TickTock == 120 then
		if SpawnOccupied == false then
			if RandomSpawn ~= false then
				Event:FireServer("New NPC",RandomSpawn)
				SpawnOccupied = true
			end
		end
	end
	----
	if TickTock == 250 then
		if SpawnOccupied == false then
			if RandomSpawn ~= false then
				Event:FireServer("New NPC",RandomSpawn)
				SpawnOccupied = true
			end
		end
	end
	----
	if TickTock == 310 then
		if SpawnOccupied == false then
			if RandomSpawn ~= false then
				Event:FireServer("New NPC",RandomSpawn)
				SpawnOccupied = true
			end
		end
	end
	----
	if TickTock == 450 then
		local Boxes = nil	
		Event:FireServer("Mail Delivery",RandomSpawn)
		script["Door Chimes Various Tone Sequences Door Bell (SFX)"]:Play()
	end
	----
	if TickTock >= 501 then
		TickTock = 0
	end
	
end
2 Likes

Could you define Value1 and Value2?

1 Like

Value 1 and value 2 are just pass-through slots for information. Pertinent to this function of the event they are:

Value1 = The physical route model, rather than the clone of the original as defined in the server-side function that calls this message.

Value2 = The number of boxes the server-side script has spawned into the world. This is so the script doesn’t activate more package receptacles than the player has boxes for.

Here is the calling function in the server-side script.

-- These variables are posted at the beginning of the script. 
RepStore                      = game.ReplicatedStorage
Serverstore                   = game.ServerStorage
Event                         = RepStore.MailEvent
Tote                          = Serverstore.MailTote
TotalBoxes                    = 0

Event.OnServerEvent:Connect(function(Player,Message,Value1,Value2,Value3,Value4)
	
-- Event section has been trimmed to only show the relevant section.

	if Message == "Route" then
		local Mailvan       = RepStore.CanadaPostVan:Clone()
		local RouteNumber   = math.random(RouteCount)
		local Route         = RepStore.MailRoutes:FindFirstChild(RouteNumber)
		local OutTote       = Serverstore.OutboundTote:Clone()
		------------------------------------------------------------
		Mailvan.Name = Player.Name.."'S Mailvan"
		Mailvan.Parent = game.Workspace
		----
		if Value1 == 1 then -- Residential Deliveries
			OutTote.Parent = game.Workspace
			Mailvan.Body.Box.ProximityPrompt.Enabled = true
			Mailvan.Body.Tote.ProximityPrompt.Enabled = true
			Player.Character.Humanoid:Move(Mailvan.Body.VehicleSeat.Position)
			----
			if Route then
				local RouteClone = Route:Clone()
				RouteClone.Parent = game.Workspace
				OutbountMail()
				Event:FireClient(Player,"RouteGen",RouteClone,TotalBoxes)
			end
			----
			for index, Object in pairs(Serverstore.Outbound:GetChildren()) do
				local CoinFlip = math.random(2)
				if CoinFlip == 2 then
					local spawned = Object:Clone()
					spawned.Parent = game.Workspace
				end
			end
		end
		----
		if Value1 == 2 then -- Post Office Deliveries
			local carts = Serverstore.DCarts:Clone()
			-----------------------------------------------
			carts.Parent = game.Workspace
		end
	end
end)
if Message == "Route" then
		local Mailvan       = RepStore.CanadaPostVan:Clone()
		local RouteNumber   = math.random(RouteCount)
		local Route         = RepStore.MailRoutes:FindFirstChild(RouteNumber)
		local OutTote       = Serverstore.OutboundTote:Clone()
		------------------------------------------------------------
		Mailvan.Name = Player.Name.."'S Mailvan"
		Mailvan.Parent = game.Workspace
		----
		if Value1 == 1 then -- Residential Deliveries
			OutTote.Parent = game.Workspace
			Mailvan.Body.Box.ProximityPrompt.Enabled = true
			Mailvan.Body.Tote.ProximityPrompt.Enabled = true
			Player.Character.Humanoid:Move(Mailvan.Body.VehicleSeat.Position)
			----
			if Route then
				local RouteClone = Route:Clone()
				RouteClone.Parent = game.Workspace
				OutbountMail()
				Event:FireClient(Player,"RouteGen",RouteClone,TotalBoxes)
			end
			----
			for index, Object in pairs(Serverstore.Outbound:GetChildren()) do
				local CoinFlip = math.random(2)
				if CoinFlip == 2 then
					local spawned = Object:Clone()
					spawned.Parent = game.Workspace
				end
			end
		end
		----
		if Value1 == 2 then -- Post Office Deliveries
			local carts = Serverstore.DCarts:Clone()
			-----------------------------------------------
			carts.Parent = game.Workspace
		end
	end

So you’ve made checks to see if it’s gone past this:

Statement and it just breaks right after that, am I correct?

Also sorry for the little late response

When I did checks to see where its breaking, it doesnt go beyond this line
“if address:IsA(“BasePart”) then”
Essentially it acts as if the “GetDescendants” statement, is a “GetChildren” statement, or rather thats the best way I can explain this.

When I looked both server side and client side to see if maybe objects werent being sent over, everything was where they are supposed to be. So its not just a case of missing assets.

1 Like

I think I see what’s going on. You’re attempting to send an instance with a RemoteEvent which isn’t gonna work.

So it was working actually, and when I print the values it shows up. In fact, the local script reads enough into that object that it prints the name of the first item it indexes.

1 Like

Well then, that was interesting.

Oh uhh well then I don’t know. It looks like it should work. Weird, maybe I’m not reading something correctly. Well good luck!

Why is the code inside OnServerEvent repeated at the end? Does that show in the studio or you made a mistake when pasting the script?

I have no idea where you are refering to in the script.

sorry, i edited my message becuase i just noticed smth

Can you highlight the section you are refering to because I dont see it.

this part of the server script when you posted it

That is just a section of the main script.

Here is what the entire remote event funtion looks like

Event.OnServerEvent:Connect(function(Player,Message,Value1,Value2,Value3,Value4)
	
	if Message == "GiveTote" then
		local PlayerTote = Tote:Clone()
		PlayerTote.Parent = Player.Character
	end
	----
	if Message == "Give Box" then
		local PlayerTote = Serverstore.Package:Clone()
		PlayerTote.Parent = Player.Character
		Value1.Union.ProximityPrompt.Enabled = true
	end
	----
	if Message == "Generate Outbound" then
		OutbountMail()
	end
	----
	if Message == "Application" then
		table.insert(Workers,Player.UserId)
		Player.RespawnLocation = game.Workspace.CanadaPost
		Player:LoadCharacter()
		wait(5)
		local UI = RepStore.MailJobs:Clone()
		UI.Parent = Player.PlayerGui
	end
	----
	if Message == "Resignation" then
		for i, c in ipairs(Workers) do
			if c == Player.UserId then
				table.remove(Workers,i)
			end
		end
		--
		if Sorting == Player.UserId then 
			Sorting = nil
		end
		--
		for i, c in ipairs(Delivery) do
			if c == Player.UserId then
				table.remove(Delivery,i)
			end
		end
	end
	----
	if Message == "Assign" then
		if Value1 == "Sorting" then
			if Sorting == nil then
				Sorting = Player.UserId
				Event:FireClient(Player,"Done")
				local Cart = Serverstore.SortingCart:Clone()
				Cart.Parent = game.Workspace
			else
				Event:FireClient(Player,"Full")
			end
		end
		----
		if Value1 == "Delivery" then
			local TaskUi = RepStore.PostalRoutes:Clone()
			----------------------------------------------------------
			TaskUi.Parent = Player.PlayerGui
			table.insert(Delivery,Player.UserId)
		end
		----
		if Value1 == "POffice" then
			table.insert(PostOffice,Player.UserId)
			local SpawnPoint = nil
			local Location = math.random(3)
			if Location == 1 then
				local Office = RepStore.MapleviewOffice:Clone()
				Player.RespawnLocation = game.Workspace:FindFirstChild("MaplerdgeSpawn")
				SpawnPoint =  game.Workspace:FindFirstChild("MaplerdgeSpawn")
				Office.Parent = game.Workspace
				Player:LoadCharacter()
			elseif Location == 2 then
				local Office = RepStore.PrinceLucasOffice:Clone()
				Player.RespawnLocation = game.Workspace:FindFirstChild("PrinceSpawn")
				SpawnPoint =  game.Workspace:FindFirstChild("PrinceSpawn")
				Office.Parent = game.Workspace
				Player:LoadCharacter()
			elseif Location == 3 then
				local Office = RepStore.WainrightOffice:Clone()
				Player.RespawnLocation = game.Workspace:FindFirstChild("WainrightSpawn")
				SpawnPoint =  game.Workspace:FindFirstChild("WainrightSpawn")
				Office.Parent = game.Workspace
				Player:LoadCharacter()
			end
			wait(5)
			Event:FireClient(Player,"NewSpawn",SpawnPoint)
		end
	end
	----
	if Message == "PrepBoxes" then
		if Value1.Name == "MPO" and MPO == false then
			MPO = true
			ReadyPost(Value1,Player)
		end
		----
		if Value1.Name == "PLO" and PLO == false then
			PLO = true
			ReadyPost(Value1,Player)
		end
		----
		if Value1.Name == "CAFO" and CAFO == false then
			CAFO = true
			ReadyPost(Value1,Player)
		end
	end
	----
	if Message == "RemoVeBoxes" then
		if Value1.Name == "MPO" and MPO == true then
			MPO = false
			ResetPost(Value1)
		end
		----
		if Value1.Name == "PLO" and PLO == true then
			PLO = false
			ResetPost(Value1)
		end
		----
		if Value1.Name == "CAFO" and CAFO == true then
			CAFO = false
			ResetPost(Value1)
		end
	end
	----
	if Message == "Mail Delivery" then
		local Boxes = Serverstore:FindFirstChild(Value1.Name):Clone()
		for index, Box in pairs(Boxes:GetChildren()) do
			if Box:IsA("Model") then
				local CoinFlip = math.random(2)
				if CoinFlip == 2 then
					local InsertModel = Box:Clone()
					InsertModel.Parent = game.Workspace
				end
			end
		end
	end
	----
	if Message == "New NPC" then
		
		GenerateNPC(Value1)
		
	end
	----
	if Message == "Destroy Me" then
		Value1:Destroy()
	end
	----
	if Message == "Route" then
		local Mailvan       = RepStore.CanadaPostVan:Clone()
		local RouteNumber   = math.random(RouteCount)
		local Route         = RepStore.MailRoutes:FindFirstChild(RouteNumber)
		local OutTote       = Serverstore.OutboundTote:Clone()
		------------------------------------------------------------
		Mailvan.Name = Player.Name.."'S Mailvan"
		Mailvan.Parent = game.Workspace
		----
		if Value1 == 1 then -- Residential Deliveries
			OutTote.Parent = game.Workspace
			Mailvan.Body.Box.ProximityPrompt.Enabled = true
			Mailvan.Body.Tote.ProximityPrompt.Enabled = true
			Player.Character.Humanoid:Move(Mailvan.Body.VehicleSeat.Position)
			----
			if Route then
				local RouteClone = Route:Clone()
				RouteClone.Parent = game.Workspace
				OutbountMail()
				Event:FireClient(Player,"RouteGen",RouteClone,TotalBoxes)
			end
			----
			for index, Object in pairs(Serverstore.Outbound:GetChildren()) do
				local CoinFlip = math.random(2)
				if CoinFlip == 2 then
					local spawned = Object:Clone()
					spawned.Parent = game.Workspace
				end
			end
		end
		----
		if Value1 == 2 then -- Post Office Deliveries
			local carts = Serverstore.DCarts:Clone()
			-----------------------------------------------
			carts.Parent = game.Workspace
		end
	end
	----
	if Message == "Reset Animation" then -- Brought in from StarterPlayer>StarterPlayerScripts>MailSystemLocal, was too lazy to do this to all the server scripts that remove the tool.
		local Humanoid = Player.Character:FindFirstChild("Humanoid")
		local Animatior = Humanoid:FindFirstChildOfClass("Animator")
		if Animatior then
			Animatior:Destroy()
			wait(.1)
			local P = Instance.new("Animator")
			P.Name = "Animator"
			P.Parent = Humanoid
		end
	end
	
end)

In the post, why is that repeated after the end)? Won’t that error in Studio?

oh.

Ya I trimmed it down to make it easier to find the section thats applicable, because this is a rather large and complicated script.

Solution Found, the script was firing the event before the server script was done generating the items. So I added task.wait(1) before the relevant section.

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