So I have this script that is suppose to “drag” a player when you click on it. But it counts the mouse at nil in the server script. Any help?
local Player = game.Players.LocalPlayer
local Character = Player.Character
local mouse = Player:GetMouse()
LOCAL
mouse.Button1Down:Connect(function()
if mouse.Target and mouse.Target.Name == 'UpperTorso' and (Player.Character.HumanoidRootPart.Position - mouse.Hit.p).magnitude <= 10 and mouse.Target.Parent.Humanoid.Health <= 15 then
game.ReplicatedStorage.DragPlayer:FireServer(mouse)
end
end)
SERVER
local re = Instance.new("RemoteEvent",game.ReplicatedStorage)
re.Name = "DragPlayer"
re.OnServerEvent:Connect(function(player, mouse)
local mouse = player:GetMouse()
print("server event")
local character = player.Character
print("got character")
local boi = Instance.new("RopeConstraint")
print("made first rope")
local rat = Instance.new("Attachment")
rat.Parent = character.UpperTorso
rat.Name = "Attachment0"
print("gave attachment to torso")
local rat2 = Instance.new("Attachment")
rat2.Parent = mouse.Target
rat2.Name = "Attachment1"
print("gave attachment to other torso")
boi.Attachment0 = character.UpperTorso.Attachment0
boi.Attachment1 = mouse.Target.Attachment1
print("got both attachments now give me rope")
end)
if mouse.Target and mouse.Target.Name == 'UpperTorso' and (Player.Character.HumanoidRootPart.Position - mouse.Hit.p).magnitude <= 10 and mouse.Target.Parent.Humanoid.Health <= 15 then
but, you forgot to put it as `if mouse.target ~= nil
i think that this is not necessary, so that might be causing the problem.
To put it simply: only LocalScripts can detect a player’s mouse (or if I’m wrong, correct me). That’s why it’ll always be nil.
I’d suggest sending the information you need from the mouse from the LocalScript to the normal script.
Your server-side script shows you’re trying to re-set the mouse variable even though you’ve already set it, but even then the server-side script wouldn’t pick it up and return nil anyways. Here’s what it should look like:
Local Script:
mouse.Button1Down:Connect(function()
if mouse.Target and mouse.Target.Name == 'UpperTorso' and (Player.Character.HumanoidRootPart.Position - mouse.Hit.p).magnitude <= 10 and mouse.Target.Parent.Humanoid.Health <= 15 then
game.ReplicatedStorage.DragPlayer:FireServer(mouse.target)
end
end)
Server-side Script
local re = Instance.new("RemoteEvent",game.ReplicatedStorage)
re.Name = "DragPlayer"
re.OnServerEvent:Connect(function(player, target)
--All your other code here
boi.Attachment1 = target.Attachment1
print("got both attachments now give me rope")
end)
And make sure to remove this bit in the server script:
mouse.Button1Down:Connect(function()
if mouse.Target ~= nil and mouse.Target.Name == 'UpperTorso' then
if (Player.Character.HumanoidRootPart.Position - mouse.Hit.p).magnitude <= 10 then
mouse.Target.Parent.Humanoid.Health <= 15 then
local character = game.Players.LocalPlayer.Character
game.ReplicatedStorage.DragPlayer:FireServer(character, mouse.Target)
end
end
end)
This is the server script:
local re = Instance.new("RemoteEvent",game.ReplicatedStorage)
re.Name = "DragPlayer"
re.OnServerEvent:Connect(function(player,character, mouse)
local boi = Instance.new("RopeConstraint")
print("made first rope")
local rat = Instance.new("Attachment")
rat.Parent = character.UpperTorso
rat.Name = "Attachment0"
print("gave attachment to torso")
local rat2 = Instance.new("Attachment")
rat2.Parent = mouse
rat2.Name = "Attachment1"
print("gave attachment to other torso")
boi.Attachment0 = character.UpperTorso.Attachment0
boi.Attachment1 = mouse.Attachment1
print("got both attachments now give me rope")
end)
You already found the mouse in your local script, so there wouldn’t be a need to do it again (and only local scripts can really use that function anyway).
That’s why you should get the target from the local script, and then send it to your normal script like I showed up above. That works because the target (assuming you’re not using local parts) is already rendered server-side and can be picked up by normal scripts.
A player’s mouse is a client-side object only so no matter what, you can’t send it to a normal script. But you can send its information like its Target.
Also now that I look closer at the code, there’s another glaring issue: Trying to put an attachment into the mouse. That will also fail since, as I stated before, the mouse is only seen client-side. Also, the fact that the RopeConstraint isn’t being parented, and you unnecessarily did a double-check for each attachment even though you already have made them and set their variables.
I just decided to go ahead and restructure it for you so get those issues out of the way, and assuming the image below is your desired result then this works:
---Assuming "Mouse" and "Player" are already pre-set variables.
local HRP = (Player.Character or Player.CharacterAdded:Wait()) and Player.Character:WaitForChild("HumanoidRootPart",10)
Mouse.Button1Down:Connect(function()
local Humanoid = Mouse.Target and Mouse.Target.Parent:FindFirstChildOfClass("Humanoid")
local Dist = (HRP.Position-Mouse.Hit.p).Magnitude
if Humanoid and Mouse.Target.Name=='UpperTorso' and Dist<=10 and Humanoid.Health<=15 then
game.ReplicatedStorage.DragPlayer:FireServer(Player.Character.UpperTorso,Mouse.Target)
end
end)
Server Script
local re = Instance.new("RemoteEvent",game.ReplicatedStorage)
re.Name = "DragPlayer"
re.OnServerEvent:Connect(function(_,UT,Target)
local boi = Instance.new("RopeConstraint",UT)
local rat = Instance.new("Attachment",UT)
rat.Name = "Attachment0"
local rat2 = Instance.new("Attachment",Target)
rat2.Name = "Attachment1"
boi.Attachment0 = rat
boi.Attachment1 = rat2
print("got both attachments now give me rope")
end)