while i was writing a system which would detect on what province a troop is standing, i have encountered a problem; for some troops the script perfectly detects on what province a troop is standing on, but for some - it does not work at all and keeps the “troopstanding” value to nil although another province which the troop is not standing on: its “troopstanding” value would change corresponding to the troop model (the “standingon” value for troops works perfectly fine but it does not correctly determine on which province it is standing on)
the “provinces” are unionoperations (for more info)
here’s the code which changes the “troopstanding” and “standingon” values (troopstanding is in province models, standingon is in troop models) in a serverscript (by raycasting):
while task.wait(0.5) do
for _, v in ipairs(workspace.Troops:GetChildren()) do
if v:FindFirstChild("HumanoidRootPart") then
local provinceVal = v.StandingOn.Value
v.StandingOn.Value = nil
if provinceVal then provinceVal.TroopStanding.Value = nil end
local params = RaycastParams.new()
local newRaycast = workspace:Raycast(v["Right Leg"].Position, v["Right Leg"].Position + Vector3.new(0, -50, 0), params)
if newRaycast then
v.StandingOn.Value = newRaycast.Instance
local province = v.StandingOn.Value
province.TroopStanding.Value = v
end
elseif v:FindFirstChild("BasePart") then
local provinceVal = v.StandingOn.Value
v.StandingOn.Value = nil
if provinceVal then provinceVal.TroopStanding.Value = nil end
local params = RaycastParams.new()
params.FilterDescendantsInstances = {v:GetDescendants()}
local newRaycast = workspace:Raycast(v.BasePart.Position, v.BasePart.Position + Vector3.new(0, -50, 0), params)
if newRaycast then
v.StandingOn.Value = newRaycast.Instance
local province = v.StandingOn.Value
province.TroopStanding.Value = v
end
end
end
end
any help would be appreciated