Hello fellow developers!
I am facing an issue while writing code for an Original “The Mimic”-Style AI.
The AI patrols around waypoints and chases any player when any are found in it’s vicinity.
ISSUE: RemoteEvent doesn’t fire when invoked to fire.
I’ve tried many solutions (WaitForChild yield, etc etc) but none of them seem to work.
This AI uses a custom State system (such as PATROLLING, CHASING, etc etc).
can you use this? I have tried the changed event. But it didn’t work, i don’t know why. If it doesn’t work then there is some problems about if statement arguments.
STATE:GetPropertyChangedSignal("Value"):Connect(function()
local val = STATE.Value
if (val == StateEnum.CHASING) and (TARGET.Value ~= nil) then
-- ...
FoundEvent:FireClient(Util:PlayerFromCharacter(TARGET.Value), true)
-- ...
else
-- ...
FoundEvent:FireAllClients(false);
-- ...
end;
end);
I would check if the conditional statement is firing since you have 2 conditions for the remoteEvent to fire.
Another question is do you have a condition at the beginning of the receiving OnServerEvent function that might be returning out of the OnServerEvent function?
If it doesn’t work, there will probably be a problem with the player (OnServerEvent), as @C0DERACTU4L said. Also you cannot get property value by function just the name itself. You should do like how i did it.
put it in OnServerEvent to above of script:
if not player then return end
Also:
STATE:GetPropertyChangedSignal("Value"):Connect(function()
local val = STATE.Value
if (val == StateEnum.CHASING) and (TARGET.Value ~= nil) then
-- ...
FoundEvent:FireClient(Util:PlayerFromCharacter(TARGET.Value), true)
-- ...
else
-- ...
FoundEvent:FireAllClients(false);
-- ...
end;
end);
Instead of using STATE, TARGET and StateEnum, I have changed it to
IsChasing: BoolValue;
Target: ObjectValue;
UPDATED CODE: which partially works
IsChasing:GetPropertyChangedSignal("Value"):Connect(function()
local val = IsChasing.Value;
if (val) then
Humanoid.WalkSpeed = ChaseSpeed.Value;
FoundEvent:FireClient(Players:GetPlayerFromCharacter(Target.Value), true)
warn("FOUND");
else
Humanoid.WalkSpeed = WalkSpeed.Value;
FoundEvent:FireAllClients(false);
end;
end);
HERE
Changed Util to the usual Players:GetPlayerFromCharacter. Error occurs there stating that FireClient: player argument must be a Player object even though the Target.Value is being set in the other parts of the code… warn("FOUND") doesn’t get printed, stating that the code yields at FoundEvent:FireClient();
Other parts of the code where Target’s value is being set
chase = function(tar: Model, player: Player):()
local path = SimplePath.new(AI, pathParams);
if ((tar) and (player)) then
local magnitude: number = nil;
repeat
magnitude = ((HumanoidRootPart.Position - Vector3.new(0, Humanoid.HipHeight, 0)) - tar.HumanoidRootPart.Position).Magnitude;
path:Run(tar.HumanoidRootPart.Position + (tar.HumanoidRootPart.AssemblyLinearVelocity/3));
-- HERE
IsChasing.Value = true;
Target.Value = tar;
if (IsTeleporting.Value) then
break;
end;
-- ...
until CONDITION;
if not (FixedPath.Value) then
randomPatrol();
else
-- TODO!
end;
end;
end;
Can you check if Target value contains the character? If not then change the objectvalue to variable in script or do this:
IsChasing:GetPropertyChangedSignal("Value"):Connect(function()
local val = IsChasing.Value;
repeat task.wait() until Target.Value
if (val) then
Humanoid.WalkSpeed = ChaseSpeed.Value;
FoundEvent:FireClient(Players:GetPlayerFromCharacter(Target.Value), true)
warn("FOUND");
else
Humanoid.WalkSpeed = WalkSpeed.Value;
FoundEvent:FireAllClients(false);
end;
end);
Could you check what the Target value is set to (either the player, character model, or a body part under the character)?
Whether or not it’s the character model, you need to make an if statement to check if that player exists before firing the RemoteEvent.
Updated Code:
IsChasing:GetPropertyChangedSignal("Value"):Connect(function()
local val = IsChasing.Value;
if (val) then
Humanoid.WalkSpeed = ChaseSpeed.Value;
local foundPlayer = Players:GetPlayerFromCharacter(Target.Value)
--[[ if the 'Target' value is set to a body part within the character,
change the first argument to 'Target.Value.Parent' in the code line above
]]
if foundPlayer then -- if player is found
FoundEvent:FireClient(foundPlayer, true)
warn("FOUND");
end
else
Humanoid.WalkSpeed = WalkSpeed.Value;
FoundEvent:FireAllClients(false);
end;
end);
The error attempt to index nil with 'Name' comes from when I print the name of the target value in the IsChasing:GetPropertychangedSignal("Value") call.
Just to be clear here, the second images “and (Target.Value ~= nil)” was added later and before the first picture, in which the error says that Attempt to index nil with 'Name'.
Currently with the if statement, it doesn’t work at all.
Is the Target value being set on the client? Because if that’s the case, that is likely why it’s able to print the value under that function (in the second image), but still being considered nil under the GetPropertyChangedSignal function. Ensure that you are setting the Target value on the server rather than the client.
If this is not the case, however, it could likely be that ‘tar’ could sometimes be nil when you call that function, or something else is setting the Target value back to nil.
But the printing worked??
The printing works, that means tar is not nil.
Also the AI is on the server and the tar and Target.value are being set on the server.
Target’s value is only set to nil when the AI jumpscares the player. (which you can see in the last screenshot.)
This is a good base, however have you tried using a listener on the client?
Now: Firing state to Clients
Possible fix: GetPropertyChangedSignal(“Value”) on client
I-
I am so thankful for your help, and others too!
Thank you guys sooo much once more! I feel so dumb even though I’ve been a developer for 3 years Why didn’t I catch that…