I’m trying to make a cloned NPC move to the player when the player gets close to the NPC but only for a single client. I have used remote events to fire the script on the client, this seems to work but the NPC still seems to move for both clients.
here is the code I’m trying to find a why to make the npc only move to the local player LOCAL SCRIPT IN PLAYERSTARTERSCRIPTS
local RS = game:GetService("ReplicatedStorage")
local ClientEvent = RS:WaitForChild("ClientEvent")
local Zombie = RS.Zombies:WaitForChild("TunnelZombie1")
local Clone = Zombie:Clone()
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 30
local temp = nil
local human = nil
local temp2 = nil
local zombie = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= Zombie) then
temp = temp2:findFirstChild("HumanoidRootPart") -- Checks for HumanoidRootPart
human = temp2:findFirstChild("Humanoid") -- Checks for Humanoid
zombie = temp2:findFirstChild("ZombieCheck") -- Triple checks for a IntValue named "ZombieCheck"
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and not zombie then -- If it has a HumanoidRootPart, A humanoid and doesnt have a ZombieCheck IntValue then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
ClientEvent.OnClientEvent:Connect(findNearestTorso)
local function ZombieWalk()
Clone.Parent = game.Workspace.ChasingZombies
Clone.HumanoidRootPart.Touched:Connect(function(hit)
local human = hit.Parent:FindFirstChild("Humanoid") -- Checks for Humanoid on hit
local zombie = hit.Parent:FindFirstChild("ZombieCheck") -- Double check so it doesnt hit a zombie
if (human ~= nil) and not zombie and human.Health ~= 0 then
human:TakeDamage(2) -- Change the amount to change the damage.
end
end)
while true do
wait(.1)
local target = findNearestTorso(Clone.HumanoidRootPart.Position)
if target ~= nil then
Clone.EnemyHumanoid:MoveTo(target.Position, target)
else
Clone.EnemyHumanoid:MoveTo(Vector3.new(-1.852, 31.754, 14.27))
end
end
end
ClientEvent.OnClientEvent:Connect(ZombieWalk)
SERVER SCRIPT IN SERVERSCRIPT SERVICE
local players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local ClientEvent = RS:WaitForChild("ClientEvent")
players.PlayerAdded:Connect(function(player)
ClientEvent:FireClient(player)
print("event fired")
print(player.Name)
end)
local RS = game:GetService("ReplicatedStorage")
local ClientEvent = RS:WaitForChild("ClientEvent")
local Zombie = RS.Zombies:WaitForChild("TunnelZombie1")
local Clone = Zombie:Clone()
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 30
local temp = nil
local human = nil
local temp2 = nil
local zombie = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= Zombie) then
temp = temp2:findFirstChild("HumanoidRootPart") -- Checks for HumanoidRootPart
human = temp2:findFirstChild("Humanoid") -- Checks for Humanoid
zombie = temp2:findFirstChild("ZombieCheck") -- Triple checks for a IntValue named "ZombieCheck"
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and not zombie then -- If it has a HumanoidRootPart, A humanoid and doesnt have a ZombieCheck IntValue then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
ClientEvent.OnClientEvent:Connect(findNearestTorso)
local function ZombieWalk()
Clone.Parent = game.Workspace.ChasingZombies
Clone.HumanoidRootPart.Touched:Connect(function(hit)
local human = hit.Parent:FindFirstChild("Humanoid") -- Checks for Humanoid on hit
local zombie = hit.Parent:FindFirstChild("ZombieCheck") -- Double check so it doesnt hit a zombie
if (human ~= nil) and not zombie and human.Health ~= 0 then
human:TakeDamage(2) -- Change the amount to change the damage.
end
end)
while true do
wait(.1)
local target = findNearestTorso(Clone.HumanoidRootPart.Position)
if target ~= nil then
Clone.EnemyHumanoid:MoveTo(target.Position, target)
else
Clone.EnemyHumanoid:MoveTo(Vector3.new(-1.852, 31.754, 14.27))
end
end
end
ClientEvent.OnClientEvent:Connect(ZombieWalk)
SERVER SCRIPT IN SERVERSCRIPTSERVICE
local RS = game:GetService("ReplicatedStorage")
local ClientEvent = RS:WaitForChild("ClientEvent")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
ClientEvent:FireServer(player)
end)
--in Server script
local RS = game:GetService("ReplicatedStorage")
local ClientEvent = RS:WaitForChild("ClientEvent")
local players = game:GetService("Players")
ClientEvent.OnServerEvent:Connect(function(player)
print("player joined")
print(player.Name)
end)
--in Local script
local RS = game:GetService("ReplicatedStorage")
local ClientEvent = RS:WaitForChild("ClientEvent")
local players = game:GetService("Players")
ClientEvent.OnClientEvent:Connect(function(player)
print("player joined")
print(player.Name)
end)
You are creating the clone in the client script. This means that it exists only on the client and not on the server.
The server script is firing the remote event to every connected client, therefore the client script is running on every client.
The way to solve this is to create the clone on the server, and then fire a remote event to the client that tells it to move the clone.