-
I want to have 1 script receive the same value of an object value that another one modified.
-
The issue is that a regular script(GunScript) cannot see what another regular script changed(Pathfinding). There are no local scripts/Client scripts, just server scripts. Here is the Pathfinding script that changes the “MovingTarget” (At line 17) object value to the HumanoidRootPart. I tested to see if the value were to go to nil but, it never did on the pathfinding script.
wait(5)
local PathFindingService = game:GetService("PathfindingService")
local DetectionRange = 200
local StaticTarget = nil
local MovingTarget = nil
local MyHuman = script.Parent:WaitForChild("Humanoid")
local CharacterTorso = script.Parent:WaitForChild("HumanoidRootPart")
while wait() do
for i, v in pairs(workspace:GetChildren()) do
local T = v:findFirstChild("HumanoidRootPart")
local human = v:findFirstChild("Humanoid")
if T and human then
if (CharacterTorso.Position - T.Position).Magnitude <= DetectionRange and MovingTarget == nil and v.Humanoid.Health > 0 and v.HumanoidRootPart.Parent.Name ~= "AI" then
MovingTarget = v.HumanoidRootPart
script.MovingTarget.Value = MovingTarget
end
end
if T and human and MovingTarget ~= nil then
local Path = PathFindingService:CreatePath({
WaypointSpacing = 16,
AgentRadius = 7,
AgentHeight = 4.5,
AgentCanJump = true,
AgentCanClimb = true,
})
Path:ComputeAsync(CharacterTorso.Position, MovingTarget.Position)
local Waypoints = Path:GetWaypoints()
Path.Blocked:Connect(function()
Path:ComputeAsync(CharacterTorso.Position, MovingTarget.Position)
local Waypoints = Path:GetWaypoints()
for i, waypoint in pairs(Waypoints) do
if script.Debug.Value == true then
local Part = Instance.new("Part")
Part.Parent = workspace
Part.Shape = "Ball"
Part.Position = waypoint.Position + Vector3.new(0, 2, 0)
Part.BrickColor = BrickColor:Random()
Part.Material = Enum.Material.Neon
Part.Size = Vector3.new(0.8, 0.8, 0.8)
Part.Anchored = true
Part.CanCollide = false
end
if waypoint.Action == Enum.PathWaypointAction.Jump and (CharacterTorso.Position - T.Position).Magnitude > 40 then
MyHuman:ChangeState(Enum.HumanoidStateType.Jumping)
end
if (CharacterTorso.Position - T.Position).Magnitude > 40 then
MyHuman.WalkSpeed = 18
MyHuman:MoveTo(waypoint.Position)
MyHuman.MoveToFinished:Wait()
else
MyHuman.WalkSpeed = 0
end
end
end)
for i, waypoint in pairs(Waypoints) do
if script.Debug.Value == true then
local Part = Instance.new("Part")
Part.Parent = workspace
Part.Shape = "Ball"
Part.Position = waypoint.Position + Vector3.new(0, 2, 0)
Part.BrickColor = BrickColor:Random()
Part.Material = Enum.Material.Neon
Part.Size = Vector3.new(0.8, 0.8, 0.8)
Part.Anchored = true
Part.CanCollide = false
end
if waypoint.Action == Enum.PathWaypointAction.Jump and (CharacterTorso.Position - T.Position).Magnitude > 40 then
MyHuman:ChangeState(Enum.HumanoidStateType.Jumping)
end
if (CharacterTorso.Position - T.Position).Magnitude > 40 then
MyHuman:MoveTo(waypoint.Position)
MyHuman.WalkSpeed = 18
MyHuman.MoveToFinished:Wait()
else
MyHuman.WalkSpeed = 0
end
end
--MyHuman:MoveTo(MovingTarget.Position)
--[[if MovingTarget.Parent:FindFirstChild("LocalScript") then -- if target is in a plane
end]]
--[[if MovingTarget.Parent:FindFirstChild("LocalScript") then -- if target is in a car
end]]
if v.Humanoid.Health < 1 or (CharacterTorso.Position - T.Position).Magnitude > DetectionRange or T.Parent:FindFirstChild("LocalScript") and MovingTarget ~= nil then
MovingTarget = nil
script.MovingTarget.Value = nil
end
end
end
end
Here is the script that is receiving the object value. The value returns nil while it should be the HumanoidRootPart that the pathfinding script changes to.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MyHuman = script.Parent:WaitForChild("Humanoid")
local Target = script.Parent.Pathfinding.MovingTarget.Value --[[It should get the value that the pathfinding script changes and then it
receive the same value instead, it is set to nil. It is like the pathfinding script is a local script while it isn't]]
local ShootingRange = 60
script.Parent.Pathfinding.MovingTarget.Changed:Connect(function()
if Target ~= nil and Target.Name == "HumanoidRootPart" and MyHuman.Parent:FindFirstChild("GlockAI") == false and (MyHuman.Parent.HumanoidRootPart.Position - Target.Position).Magnitude <= ShootingRange then
MyHuman:EquipTool(ReplicatedStorage.GlockAI)
elseif Target == nil or (MyHuman.Parent.HumanoidRootPart.Position - Target.Position).Magnitude > ShootingRange and MyHuman.Parent:FindFirstChild("GlockAI") then
MyHuman:UnequipTools(MyHuman.Parent.GlockAI)
end
end)
I don’t know if this is a Roblox bug or if it is my scripting. I always used this practice and it worked before but now it doesn’t anymore. Please help!