Im attempting to make a move where once you click on a player, youll teleport behind them and hit them, ill worry about the attack part later but the teleporting isnt working, ive looked up multiple times how to fix it but it just doesnt seem to work
LocalScript
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Remote = script.Parent:WaitForChild("RemoteEvent")
Mouse.Button1Down:Connect(function(Target)
if Mouse.Target:FindFirstChildOfClass("Humanoid") then
Remote:Fire(Player, Target)
end
end)
Server Script
local Remote = script.Parent:WaitForChild("RemoteEvent")
Remote.OnServerEvent:Connect(function(Player, Target)
local PlayerHumanoid = Player.Character:WaitForChild("Humanoid")
local TargetHumanoid = Target.Character:WaitForChild("Humanoid")
local LastTargetPosition = TargetHumanoid.RootPart.CFrame
local Length = 3 -- Length between player and target player
PlayerHumanoid.RootPart.CFrame = LastTargetPosition + LastTargetPosition.LookVector * Length
PlayerHumanoid.RootPart.CFrame = CFrame.new(PlayerHumanoid.RootPart.CFrame.Position, Vector3.new(LastTargetPosition.Position.X, PlayerHumanoid.RootPart.CFrame.Position.Y, LastTargetPosition.Position.Z))
end)
I get the error “attempt to index nil with FindFirstChildOfClass”
if someone could help me i would appreciate it
This seems more of a problem that Mouse.Target is nil itself. You need to send the Target’s Parents and also use :FireServer not just :Fire for a remote event.
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Remote = script.Parent:WaitForChild("RemoteEvent")
Mouse.Button1Down:Connect(function()
if not Mouse.Target then return end
if Mouse.Target.Parent:FindFirstChildOfClass("Humanoid") then
Remote:FireServer(Player, mouse.Target.Parent)
end
end)
Also, you would have to check the Target’s Parent, not the target itself. Firing the Target of the mouse would return the Character of the Target itself and not the player. Lastly, You are trying to find “RootPart” inside of a humanoid. which one wouldn’t be there and two would be HumanoidRootPart.
local Remote = script.Parent:WaitForChild("RemoteEvent")
Remote.OnServerEvent:Connect(function(Player, Target)
local PlayerHumanoid = Player.Character:WaitForChild("HumanoidRootPart")
local TargetHumanoid = Target:WaitForChild("HumanoidRootPart")
local LastTargetPosition = TargetHumanoid.CFrame
local Length = 3 -- Length between player and target player
PlayerHumanoid.CFrame = LastTargetPosition + LastTargetPosition.LookVector * Length
PlayerHumanoid.CFrame = CFrame.new(PlayerHumanoid.CFrame.Position, Vector3.new(LastTargetPosition.Position.X, PlayerHumanoid.CFrame.Position.Y, LastTargetPosition.Position.Z))
end)
Mouse.Button1Down:Connect(function(Target)
if Mouse.Target and Mouse.Target.Parent and Mouse.Target.Parent:FindFirstChildOfClass("Humanoid") then
Remote:Fire(Player, Mouse.Target.Parent)
end
end)
This fired the remote event but it does not run the server script, i tried using prints to check where it stops, but it just doesnt seem to run after the Remote connects on the server script (I did use the server script you sent)
Where are the scripts located?
Edit: There is also a problem on my part, I forgot to remove one of the sent parameters. We need to remove the Player parameter from the :FireServer because the player that fired the event is always passed and will always be the first received parameter of the remote event.
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Remote = script.Parent:WaitForChild("RemoteEvent")
Mouse.Button1Down:Connect(function()
if not Mouse.Target then return end
if Mouse.Target.Parent:FindFirstChildOfClass("Humanoid") then
Remote:FireServer(mouse.Target.Parent)
end
end)
Nvm, I changed some parts and figured out that MouseButton1Down was messing with it
Local Script
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
script.Parent.Activated:Connect(function()
if not Mouse.Target then return end
if Mouse.Target.Parent:FindFirstChildOfClass("Humanoid") then
script.Parent.RemoteEvent:FireServer(Mouse.Target.Parent)
end
end)
Server Script
local Remote = script.Parent:WaitForChild("RemoteEvent")
Remote.OnServerEvent:Connect(function(Player, Target)
local PlayerHumanoid = Player.Character:WaitForChild("HumanoidRootPart")
local TargetHumanoid = Target:WaitForChild("HumanoidRootPart")
local LastTargetPosition = TargetHumanoid.CFrame
local Length = 3 -- Length between player and target player
PlayerHumanoid.CFrame = LastTargetPosition + LastTargetPosition.LookVector * Length
PlayerHumanoid.CFrame = CFrame.new(PlayerHumanoid.CFrame.Position, Vector3.new(LastTargetPosition.Position.X, PlayerHumanoid.CFrame.Position.Y, LastTargetPosition.Position.Z))
end)
--LOCAL
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Tool = script.Parent
local Remote = Tool:WaitForChild("RemoteEvent")
Tool.Activated:Connect(function()
if not Mouse.Target then
return
end
if Mouse.Target.Parent:FindFirstChildOfClass("Humanoid") then
Remote:FireServer(Mouse.Target.Parent)
end
end)
--SERVER
local Tool = script.Parent
local Remote = Tool:WaitForChild("RemoteEvent")
Remote.OnServerEvent:Connect(function(Player, Target)
local Character = Player.Character
local HRP = Character.HumanoidRootPart
local TargetHRP = Target.HumanoidRootPart
local Distance = Player:GetDistanceFromCharacter(TargetHRP.Position)
HRP.CFrame = TargetHRP.CFrame + TargetHRP.CFrame.LookVector * Distance
HRP.CFrame = CFrame.new(HRP.CFrame.Position, Vector3.new(TargetHRP.CFrame.Position.X, HRP.CFrame.Position.Y, TargetHRP.CFrame.Position.Z))
end)
Fixed up a few things, specifically the distance checking (you may want to merge both “CFrame” property assignments into one as currently the second will override the first).