Hello! I tried to make a Handcuff-System but it can’t find the Humanoid of the player, as it doesn’t print “Server found humanoid”. What’s wrong with this? How can I fix it? Thank you for your answers!
--//Local Script//--
local Handcuff = script.Parent
local plr = game.Players.LocalPlayer
local CufferChar = plr.CharacterAdded:Wait()
local RS = game:GetService("ReplicatedStorage")
local HumCheck = RS.HumanoidCheck
Handcuff.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
if mouse.Target ~= nil then
print("Target is not nil.") --Gets printed!!!!!
local mouseTarget = mouse.Target
HumCheck:FireServer(mouseTarget, CufferChar)
print("Server fired!") --Gets printed!!!!!
end
end)
end)
--//Server Script//--
local RS = game:GetService("ReplicatedStorage")
local HumCheck = RS.HumanoidCheck
HumCheck.OnServerEvent:Connect(function(plr, mouseTarget, CufferChar)
print("I am the server and I just got fired!") --Doesn't get printed
if mouseTarget.Parent:FindFirstChild("Humanoid") then
print("Server found humanoid") --Doesn't get printed
local TargetHum = mouseTarget.Parent:FindFirstChild("Humanoid")
local CufferHumRootPart = CufferChar:FindFirstChild("HumanoidRootPart")
TargetHum:MoveTo(CufferHumRootPart.Position)
end
end)
Read what the page about mouse.Target says the item is not replicated, that means that when you give it trough event it will become nil, that’s the reason, give the humanoid instead.
mouse.Target is a reference to an instance, so whether the actual instance it points to replicates or not falls under standard replication rules. If the client creates it, the server will see it as nil because it doesn’t exist on the server.
I’m also not too sure why this was marked as the solution if it doesn’t actually solve the problem (@GEILER123456). The only relevance mouse.Target would have to this situation is whether or not the mouseTarget parameter in the OnServerEvent function is nil or not, but OP is saying that the remote prints aren’t firing. If that’s the case, then this has no relation to the issue OP is facing.
More information is needed. The content of the server script isn’t invalid, so there must be another underlying problem here. For starters, a question that should be asked, where is that script even located? If the function does nothing, then either
A) The remote is not getting fired, but judging by the comments of the LocalScript it does.
B) The script is in a location where it doesn’t run in.
--//Local Script//--
local Handcuff = script.Parent
local plr = game.Players.LocalPlayer
local CufferChar = plr.CharacterAdded:Wait()
local RS = game:GetService("ReplicatedStorage")
local CufferRootPart = CufferChar:FindFirstChild("HumanoidRootPart")
local CuffPlayer = RS.CuffPlayer
Handcuff.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
if mouse.Target ~= nil then
local mouseTarget = mouse.Target
local TargetHum = mouseTarget.Parent:FindFirstChild("Humanoid")
if TargetHum then
CuffPlayer:FireServer(TargetHum, CufferRootPart)
end
end
end)
end)
--//Server Script//--
local RS = game:GetService("ReplicatedStorage")
local CuffPlayer = RS.CuffPlayer
CuffPlayer.OnServerEvent:Connect(function(plr, TargetHum, CufferRootPart)
TargetHum:MoveTo(CufferRootPart.Position)
end)
I’m aware of what the thread said. mouse.Target’s behaviour is irrelevant to the issue and would only relate to what the server gets back as the mouseTarget parameter.
@GEILER123456 Please review my post and answer the questions there then. Also, please do not mark solutions if they do not solve your problem. This is confusing to other users who may be running into similar problems and see your thread looking for a solution to resolve their own cases.
I commented the ones that did get printed out. It was only the “Target is not nil” one.
--//Local Script//--
local Handcuff = script.Parent
local plr = game.Players.LocalPlayer
local CufferChar = plr.CharacterAdded:Wait()
local RS = game:GetService("ReplicatedStorage")
local CufferRootPart = CufferChar:FindFirstChild("HumanoidRootPart")
local CuffPlayer = RS.CuffPlayer
Handcuff.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
if mouse.Target ~= nil then
print("Target is not nil") --Gets printed!
local mouseTarget = mouse.Target
local TargetHum = mouseTarget.Parent:FindFirstChild("Humanoid")
if TargetHum then
print("Hum found") --Doesn't get printed!
CuffPlayer:FireServer(TargetHum, CufferRootPart)
print("Event fired from local script") --Doesn't get printed!
end
end
end)
end)
--//Server Script//--
local RS = game:GetService("ReplicatedStorage")
local CuffPlayer = RS.CuffPlayer
CuffPlayer.OnServerEvent:Connect(function(plr, TargetHum, CufferRootPart)
print("Server got fired") --Doesn't get printed!
TargetHum:MoveTo(CufferRootPart.Position)
print("Target should move to the Cuffer Position") --Doesn't get printed!
end)
So these aren’t quite my words, but something was raised to me privately so I’m just relaying it here. The code you wrote isn’t invalid but certain parts of your code aren’t getting reached and there are no errors here. Leads me (us) to believe that your testing method is just wrong.
Could you perhaps use print statements to actually check what you’re accessing instead of just to what parts of your code are getting reached? For example, from the Tool LocalScript, you have it so that the remote is only fired if there’s a Humanoid. Could you then print what TargetHum is just above the if statement?
print(TargetHum.ClassName, TargetHum)
if TargetHum then
What could be the problem is that you’re clicking on an accessory, so naturally the parent of the accessory’s handle wouldn’t have a Humanoid. If this print statement gives you an accessory, that’s your problem. You can resolve this in a few ways. My personal favourite, although it may not be the best, is to check if the part belongs to a character and find the Humanoid from there.
local Players = game:GetService("Players")
local function getCharacterHumanoid(part)
for _, player in ipairs(Players:GetPlayers()) do
local character = player.Character
if character and part:IsDescendantOf(character) then
return character:FindFirstChildOfClass("Humanoid")
end
end
return nil
end
If you use this function, then TargetHum can be assigned to the return value of this function instead of trying to do a raw finding of their Humanoid. So in your code sample, it’d be:
local TargetHum = getCharacterHumanoid(mouseTarget)
if TargetHum then
Hey if it’s not finding the humanoid maybe somehow it’s pointing to something that’s not directly under character, try this :
local targetHum = mouseTarget and mouseTarget.Parent and (mouseTarget.Parent:FindFirstChildOfClass("Humanoid") or mouseTarget.Parent.Parent and mouseTarget.Parent.Parent:FindFirstChildOfClass("Humanoid"))
I think you should use a ray instead of mouse.Target that doesn’t look so common.
local ray = Ray.new(player.Character.Head.CFrame.p, (mouse.Hit.p - player.Character.Head.CFrame.p).unit * MaxCuffDistance)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
if part.Parent:FindFirstChild("Humanoid") then
TargetHum = part.Parent:FindFirstChild("Humanoid")
end