Script is not changing int value

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to make an AI attack on any amount of cores.

  2. What is the issue? This script is not changing the defense value.

  3. What solutions have you tried so far? I tried debugging by putting print statements (Shown in code) and nothing is printed to the console until I delete all the cores. When I do this happens:


an endless amount of errors.

Here is my code

  local hrp = script.Parent.Parent.HumanoidRootPart -- Humanoid Root Part
local hum = script.Parent.Parent.Humanoid
local pathservice = game:GetService("PathfindingService")
local params = {
	AgentRadius = 2.5,
	AgentHeight = 4.5,
	AgentCanJump = false
}
local path = pathservice:CreatePath(params)
function getNewDes()
	local closest = math.huge
	local closestObj = nil

	for _, obj in pairs(workspace:GetDescendants()) do
		if obj.Name:lower():find("core") and obj:IsA("Part") then
			if (obj.Position - hrp.Position).Magnitude < closest then
				closestObj = obj
				closest = (closestObj.Position - hrp.Position).Magnitude
			end
		end
	end
	return closestObj
end

function move()
	local lastwaypoint = false
	function zombieStuck()
	local position1 = hrp.Position.Magnitude
	wait(1)
	local position2 = hrp.Position.Magnitude
	local deltaPosition = (math.abs(position1 - position2)) 
	if deltaPosition < 1 then
			path:Destroy()
			lastwaypoint = true
			wait()
			for _, I in pairs(game.Workspace.Waypoints:GetDescendants()) do
				I:Destroy()
			end
			script.bool.Value = true
			hum:MoveTo(hrp.Position)
			wait()
		move()
		
	end
end
	


path:ComputeAsync(hrp.Position, getNewDes().Position)

	path.Blocked:Connect(function()
		lastwaypoint = true
		for _, I in pairs(game.Workspace.Waypoints:GetDescendants()) do
			I:Destroy()
		end
		script.bool.Value = true
		move()
	end)
	

local waypoints = path:GetWaypoints()
	for _, waypoint in pairs(waypoints) do
		local part = Instance.new("Part")
		part.Shape = "Ball"
		part.Material = "Neon"
		part.Size = Vector3.new(0.6, 0.6, 0.6)
		part.Position = waypoint.Position
		part.Anchored = true
		part.CanCollide = false
		part.Parent = game.Workspace.Waypoints
	end
	for _, waypoint in pairs(waypoints) do
		
		if waypoints[#waypoints] == waypoint then
			lastwaypoint = true
		end
		if lastwaypoint == true then
			break
		end
		-- Move zombie to the next waypoint
		hum:MoveTo(waypoint.Position)
		if lastwaypoint == true then
			break
		end
		-- Wait until zombie has reached the waypoint before continuing
		hum.MoveToFinished:Wait()
		if lastwaypoint == true then
			break
		end
		
	end
	repeat 
			zombieStuck()
			wait(1)
	until las
	
	while wait() do
		if lastwaypoint == true then
			for _, I in pairs(game.Workspace.Waypoints:GetDescendants()) do
				I:Destroy()
			end
			script.bool.Value = true
			break
		end
	end
		
	
	
end

	
local count = 0
for _,v in pairs(game.Workspace.Cores:GetChildren()) do
	count = count + 1
end
print(count)
local count2 = 0

repeat
	wait()
	count2 = count2 + 1
	move()
	repeat
		wait()
	until script.bool.Value == true
	
	local part = getNewDes()
	local Defense = part.Defense.Value
	print('1')
	repeat
		print('2')
		wait(.5)
		Defense = Defense - script.Parent.Parent.Attack.Value
	until Defense <= 0
	print('3')
	while wait() do
		print('4')
		if Defense <= 0 then
			print('5')
			part:Destroy()
			break
		end
	end
until count2 == count

All help is appreciated!!!

Sorry I’m too lazy to count to line 49 so can you show me which line it is :stuck_out_tongue:

You could always open wordpad :wink:

1 Like

try changing
local hrp = script.Parent.Parent.HumanoidRootPart
to:
local hrp = script.Parent.Parent:WaitForChild("HumanoidRootPart")

1 Like

Well the humanoid is a dummy

image

But I will try

if it doesnt work try changing this:

	for _, obj in pairs(workspace:GetDescendants()) do
		if obj.Name:lower():find("core") and obj:IsA("Part") then
			if (obj.Position - hrp.Position).Magnitude < closest then
				closestObj = obj
				closest = (closestObj.Position - hrp.Position).Magnitude
			end
		end
	end

to this:

	for _, obj in pairs(workspace:GetDescendants()) do
		if obj.Name:lower():find("core") and obj:IsA("Part") then
			closestObj = obj
			closest = (closestObj.Position - hrp.Position).Magnitude
		end
	end
1 Like

Update: I updated the code, so on line 126 I added image
This now makes “3” come up in the console. (“3” is part of the debugging)

Why? This causes more errors now

Huge update, I fixed my incomplete code
image
to
image

Now takes one core, but still stuck on the rest…

Studio sometimes does this really weird thing in which when you attempt to change an Object that has a Value property by directly stating it in a variable, it won’t actually change it. So instead of doing something like this:

local MyIntValue = script.Parent:WaitForChild("IntValue").Value

You would do this

local MyIntValue = script.Parent:WaitForChild("IntValue")

Then make changes to it later, so like this basically

MyIntValue.Value = MyIntValue.Value + 1

or other similar things. I didn’t read the full thing because your code is a little bit beyond my understanding but by judging from the title, it won’t change so I decided I would put this here.

2 Likes

You are the best. Who knew this was a small glitch the whole time! Thanks a lot!

1 Like

I think it usually has something to do with finding an actual child of the part rather than the actual value itself. using . to ascend or descend can mean property or instance so I guess it gets confused.

Glad this small mention helped your issue :slight_smile:

1 Like