How to optimise this script?

Hi!

So I’ve made some river rapids. This script uses Zone Plus by Nanoblox. It’s a great resource. The only problem is my inability to script well.

I’m sure you can notice that’ I’ve got a baisc knowledge of things, but all of the functions are seperate, and I’ll have to make an entry and exit function for all of the individual segments.

I’m wondering if there’s a way to shorten this, or have a universal ‘ExitZone’ function for all of the zones included?

local Zone = require(game:GetService("ReplicatedStorage").Zone)
local ZoneController = require(game:GetService("ReplicatedStorage").Zone.ZoneController)


local container4 = workspace.RapidsD
local container5 = workspace.RapidsE
local container6 = workspace.RapidsF
local container7 = workspace.RapidsG


local zoned = Zone.new(container4)
local zonee = Zone.new(container5)
local zonef = Zone.new(container6)
local zoneg = Zone.new(container7)

zoned.playerEntered:Connect(function(plr)
	for _,v in pairs(plr.Character:GetChildren()) do
		if plr.Character.HumanoidRootPart:FindFirstChild("Current")==nil then
			local b = Instance.new("BodyForce")
			b.Parent = plr.Character.HumanoidRootPart
			b.force = Vector3.new(1000, 0, -500)
			b.Name = "Current"
			b.ApplyAtCenterOfMass = true
		end
	end
end)
zonee.playerEntered:Connect(function(plr)
	for _,v in pairs(plr.Character:GetChildren()) do
		if plr.Character.HumanoidRootPart:FindFirstChild("Current")==nil then
			local b = Instance.new("BodyForce")
			b.Parent = plr.Character.HumanoidRootPart
			b.force = Vector3.new(1000, 0, 0)
			b.Name = "Current"
			b.ApplyAtCenterOfMass = true
		end
	end
end)
zonef.playerEntered:Connect(function(plr)
	for _,v in pairs(plr.Character:GetChildren()) do
		if plr.Character.HumanoidRootPart:FindFirstChild("Current")==nil then
			local b = Instance.new("BodyForce")
			b.Parent = plr.Character.HumanoidRootPart
			b.force = Vector3.new(0, 0, 1000)
			b.Name = "Current"
			b.ApplyAtCenterOfMass = true
		end
	end
end)
zoneg.playerEntered:Connect(function(plr)
	for _,v in pairs(plr.Character:GetChildren()) do
		if plr.Character.HumanoidRootPart:FindFirstChild("Current")==nil then
			local b = Instance.new("BodyForce")
			b.Parent = plr.Character.HumanoidRootPart
			b.force = Vector3.new(1000, 0, 0)
			b.Name = "Current"
			b.ApplyAtCenterOfMass = true
		end
	end
end)


zoned.playerExited:Connect(function(plr)
	for _,v in pairs(plr.Character:GetChildren()) do
		if plr.Character.HumanoidRootPart:FindFirstChild("Current") then
			plr.Character.HumanoidRootPart.Current:Destroy()
		end
	end
	
end)
zonee.playerExited:Connect(function(plr)
	for _,v in pairs(plr.Character:GetChildren()) do
		if plr.Character.HumanoidRootPart:FindFirstChild("Current") then
			plr.Character.HumanoidRootPart.Current:Destroy()
		end
	end

end)
zonef.playerExited:Connect(function(plr)
	for _,v in pairs(plr.Character:GetChildren()) do
		if plr.Character.HumanoidRootPart:FindFirstChild("Current") then
			plr.Character.HumanoidRootPart.Current:Destroy()
		end
	end

end)
zoneg.playerExited:Connect(function(plr)
	for _,v in pairs(plr.Character:GetChildren()) do
		if plr.Character.HumanoidRootPart:FindFirstChild("Current") then
			plr.Character.HumanoidRootPart.Current:Destroy()
		end
	end

end)
1 Like

i am not gonna type a lot but the basic idea is using for i,v loops and using a table to change the uhh b.Force

also belongs in #help-and-feedback:code-review

yea heck it here:

local Zone = require(game:GetService("ReplicatedStorage").Zone)
local ZoneController = require(game:GetService("ReplicatedStorage").Zone.ZoneController)


local container4 = workspace.RapidsD
local container5 = workspace.RapidsE
local container6 = workspace.RapidsF
local container7 = workspace.RapidsG


local zoned = Zone.new(container4)
local zonee = Zone.new(container5)
local zonef = Zone.new(container6)
local zoneg = Zone.new(container7)

local zones = {zoned,zonee,zonef,zoneg}
local forcetable = {[zoned] = Vector3.new(1000,0,-500),[zonee] = Vector3.new(1000,0,0),[zonef] = Vector3.new(0,0,1000),[zoneg] = Vector3.new(1000,0,0)}

for i,v in pairs(zones) do
	v.playerEntered:Connect(function(plr)
		for _,v in pairs(plr.Character:GetChildren()) do
			if plr.Character.HumanoidRootPart:FindFirstChild("Current")==nil then
				local b = Instance.new("BodyForce")
				b.Parent = plr.Character.HumanoidRootPart
				b.force = forcetable[i] -- probably will make a error here
				b.Name = "Current"
				b.ApplyAtCenterOfMass = true
			end
		end
	end)
end

for i,v in pairs(zones) do
	v.playerExited:Connect(function(plr)
		for _,v in pairs(plr.Character:GetChildren()) do
			if plr.Character.HumanoidRootPart:FindFirstChild("Current") then
				plr.Character.HumanoidRootPart.Current:Destroy()
			end
		end
	end)
end

PLEASE NOTE: I WROTE THIS IN DEVFORUM PLEASE FIX MISTAKES,

2 Likes

Yep, it works up until the b.force = forcetable[i] line. I’ll see if I can figure out how to fix that part.

And I’ll use that part of the forum for queries like this in the future, thanks!

oh here you go a fix i suppose

for name,vector in pairs(forcetable) do
if v == name then
b.force = vector
end
end

The Parent property should be the last property to be set. There’s a bunch of replication magic behind the scenes that only occurs when the object gets parented, so the server needs to send more replication requests than if the Parent property was set last.

This could all be in one function.

function destroyCurrent(plr) 
       for _,v in pairs(plr.Character:GetChildren()) do
		if plr.Character.HumanoidRootPart:FindFirstChild("Current") then
			plr.Character.HumanoidRootPart.Current:Destroy()
		end
	end
end
zonee.playerExited:Connect(destroyCurrent)
zonef.playerExited:Connect(destroyCurrent)
zoneg.playerExited:Connect(destroyCurrent)
2 Likes