Roblox instance values are not working when a server script changes the value

  1. I want to have 1 script receive the same value of an object value that another one modified.

  2. 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!

3 Likes

It depends where the value is instanced. Create the value from within a server script and any other server scripts should be able to see it. Other than that send events to the other script and retrieve the value.

3 Likes

I tried “instance.new()” it still doesn’t work and tried placing it at other places in the script. Thank you for trying to help!

1 Like

After you instance.new() it where are you parenting it to?

1 Like

I now have it parenting to the character instead of the server script. I know it would be able to work anywhere as long as it is in the character.

I’m not really sure if I fully understand, but would it be better to place your MovingTarget in ReplicatedStorage? Then make Pathfinding edit the value, and then you can use .Changed on it from ReplicatedStorage

Also, what kind of Value Object is that, that you’re using?

1 Like

It needs to be in the AI because I want the AI to target different people and the scripts need to have the correct value within the AI. The Object Value is connected to the humanoid it is targeting to. By the way, I fixed this somehow by having the variable set to the instance instead of the value. Thank you, everyone, for the help!

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