You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want to make a game like https://Territorial.io and I want to find parts near the parts and change their color. -
What is the issue? Include screenshots / videos if possible!
whenever my script find a part it continues on and on and a ton of ray casts cause a ton of lag. e.g. if I want to expand it will just expand to the part right, left, up, down parts
like this;
and when you expand again it’ll look like this and so on
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Code 1:
for i,v in pairs(workspace.Europe:GetChildren()) do
if v:IsA("BasePart") and v:GetAttribute("Owner") == plr.Name and v:FindFirstChild("Done") == nil then
local inst = Instance.new("BoolValue")
inst.Name = "Done"
inst.Parent = v
local function ray1()
-- Set an origin and directional vector
local rayOrigin = v.Position
local rayDirection = Vector3.new(0, 0, -0.3)
-- Build a "RaycastParams" object and cast the ray
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {plr.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
-- Check if the part resides in a folder, that it's fully visible, and not locked
if hitPart:GetAttribute("Owner") == plr.PlayerValues.Attacking.Value then
plr.PlayerValues.Pixels.Value += 1
hitPart.Color = plr.PlayerValues.ColorValue.Value
hitPart:SetAttribute("Owner",plr.Name)
end
end
end
local function ray2()
-- Set an origin and directional vector
local rayOrigin = v.Position
local rayDirection = Vector3.new(0, 0, 0.3)
-- Build a "RaycastParams" object and cast the ray
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {plr.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
-- Check if the part resides in a folder, that it's fully visible, and not locked
if hitPart:GetAttribute("Owner") == plr.PlayerValues.Attacking.Value then
plr.PlayerValues.Pixels.Value += 1
hitPart.Color = plr.PlayerValues.ColorValue.Value
hitPart:SetAttribute("Owner",plr.Name)
end
end
end
local function ray3()
-- Set an origin and directional vector
local rayOrigin = v.Position
local rayDirection = Vector3.new(-0.3, 0, 0)
-- Build a "RaycastParams" object and cast the ray
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {plr.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
-- Check if the part resides in a folder, that it's fully visible, and not locked
if hitPart:GetAttribute("Owner") == plr.PlayerValues.Attacking.Value then
plr.PlayerValues.Pixels.Value += 1
hitPart.Color = plr.PlayerValues.ColorValue.Value
hitPart:SetAttribute("Owner",plr.Name)
end
end
end
local function ray4()
-- Set an origin and directional vector
local rayOrigin = v.Position
local rayDirection = Vector3.new(0.3, 0, 0)
-- Build a "RaycastParams" object and cast the ray
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {plr.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
-- Check if the part resides in a folder, that it's fully visible, and not locked
if hitPart:GetAttribute("Owner") == plr.PlayerValues.Attacking.Value then
plr.PlayerValues.Pixels.Value += 1
hitPart.Color = plr.PlayerValues.ColorValue.Value
hitPart:SetAttribute("Owner",plr.Name)
end
end
end
ray1(); ray3();
ray2(); ray4();
end
end
for i,v in pairs(workspace.Europe:GetChildren()) do
if v:IsA("BasePart") and v:FindFirstChild("Done") then
v:WaitForChild("Done"):Destroy()
end
end
Code 2:
local p = nil
for i,e in pairs(workspace.Europe:GetChildren()) do
if e:IsA("BasePart") and e:GetAttribute("Owner") == plr.Name then
p = e
end
end
for i,v in pairs(workspace.Europe:GetChildren()) do
if v:IsA("BasePart") then
if v:GetAttribute("Owner") ~= plr.Name then
for i,e in pairs(workspace.Europe:GetChildren()) do
if e:IsA("BasePart") and e:GetAttribute("Owner") == plr.Name then
p = e
end
end
local magn = (p.Position - v.Position).Magnitude
if magn <= 0.6 then
plr.PlayerValues.Pixels.Value += 1
v.Color = plr.PlayerValues.ColorValue.Value
v:SetAttribute("Owner",plr.Name)
end
end
end
end
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
en