Need help for a simple script #2

Hello again
image
I have a killer AI. Target value is an object value and it shows what does it chase at the moment. Just like this:
image

Script checks if the AI has a target. If yes, it increases the speed. If no, keeps the speed at deafult.
local chasing = script.Parent --value
chasing.Changed:Connect(function(newValue) --check if value changed
	if newValue == "Humanoid" then --bruh string
		print("asd")
		script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 20 --change speed if has a target
		if newValue == nil then
			print("dsa")
			script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 8  --change speed back to normal if lost the target
       end
	end
end)

Well,script doesn’t work. (probably because I wrote the humanoid like a string)
How do I solve this problem?
Thank you.

local chasing = script.Parent --value
chasing.Changed:Connect(function(newValue) --check if value changed
	if newValue == "Humanoid" then --bruh string
		print("asd")
		script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 20 --change speed if has a target
		if newValue == nil then
			print("dsa")
			script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 8  --change speed back to normal if lost the target
       end
	end
end)

you didn’t put a end for newvalue == nil

thank you but still doesn’t work :wut:

local chasing = script.Parent --value
chasing.Changed:Connect(function(newValue) --check if value changed
	if newValue.Name == "Humanoid" then --bruh string
		print("asd")
		script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 20 --change speed if has a target
		if newValue == nil then
			print("dsa")
			script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 8  --change speed back to normal if lost the target
       end
	end
end)

Or you could do this

local chasing = script.Parent --value
chasing.Changed:Connect(function(newValue) --check if value changed
	if newValue:IsA("Humanoid") then --bruh string
		print("asd")
		script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 20 --change speed if has a target
		if newValue == nil then
			print("dsa")
			script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 8  --change speed back to normal if lost the target
       end
	end
end)
1 Like

Try using chasing:GetPropertyChangedSignal("Value"):Connect() maybe?

local chasing = script.Parent --value
chasing:GetPropertyChangedSignal("Value"):Connect(function(newValue) --check if value changed
	if newValue == "Humanoid" then --bruh string
		print("asd")
		script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 20 --change speed if has a target
	elseif newValue == nil then
			print("dsa")
			script.Parent.Parent.Parent.Parent.Humanoid.WalkSpeed = 8  --change speed back to normal if lost the target
       end
	end
end)
1 Like

Tried, didn’t work. qwfsdfgdffadf

I tried this before but doesn’t work.

if newValue.Name == "Humanoid" then

I also tried this since it’s an object value, not a string.

I’ll rewrite this for you.

script.Parent:GetPropertyChangedSignal("Value"):Connect(function()
    local humanoid = script.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")
    if humanoid then
        if script.Parent.Value == "Humanoid" then
            humanoid.WalkSpeed = 20
        else
            humanoid.WalkSpeed = 8
        end
    end
end)
1 Like

HEY, Thank you.
With a few changes, your script worked.

script.Parent:GetPropertyChangedSignal("Value"):Connect(function()
	local humanoid = script.Parent.Parent.Parent:FindFirstChild("Humanoid") --fixed humanoid link
	if humanoid then
		if script.Parent.Value.Name == "Humanoid" then --added .Name
			humanoid.WalkSpeed = 20
		else
			humanoid.WalkSpeed = 8
		end
	end
end)

Like all “-Value” objects, this single value is stored in the Value property. The Changed event for this (and other objects like it) will fire with the new value being stored in the object, instead of a string representing the property being changed.

No need to use GetPropertyChangedSignal as ObjectValue has its own Changed event that only fires when its Value property changes and returns its exact value in the first paramater

local Chasing = script.Parent
Chasing.Changed:Connect(function(Target)
	local Humanoid = Chasing.Parent.Parent:FindFirstChild("Humanoid")
	if not Humanoid then return end
	
	if Target and Target.ClassName == "Humanoid" then
		Humanoid.WalkSpeed = 20
	else
		Humanoid.WalkSpeed = 8
	end
end)
1 Like