As @incapaxx said:
Player:GetMouse returns nil on the server, since the mouse instance is created locally. You can send certain properties of the mouse to the server, preferably via RemoteEvents.
The Mouse object is absent on the server, so it cannot index the instance. To access the values from the mouse, you must use RemoteEvents and/or RemoteFunctions and pass the required values while using the remote as such:
Server script in ServerScriptService
local remote = game:GetService("ReplicatedStorage"):WaitForChild("Remote", 10)
remote.OnServerEvent:Connect(function(plr, hit)
local bullet1 = Instance.new("Part")
bullet1.Size = Vector3.new(0.08, 0.05, 1)
bullet1.Material = Enum.Material.Neon
bullet1.Color = Color3.fromRGB(255, 217, 0)
bullet1.Transparency = 0
bullet1.CanCollide = false
bullet1.Name = "Bullet"
bullet1.Position = Uzi.Barrel1.Position
bullet1.Orientation = plr.Character.HumanoidRootPart.Orientation
bullet.Parent = workspace
local velocity = Instance.new("BodyVelocity")
on
velocity.Velocity = hit.LookVector*300
end)
Local script in StarterPack:
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local remote = game:GetService("ReplicatedStorage"):FindFirstChild("Remote")
local Uzi = plr.Backpack:FindFirstChild("Dual Uzis")
Uzi.Activated:Connect(function()
remote:FireServer(mouse.Hit)
end)
Alternate approach
Another option, one that can be used if multiple tools use the same remote is to change the server script to this:
local remote = game:GetService("ReplicatedStorage"):WaitForChild("Remote", 10)
remote.OnServerEvent:Connect(function(plr, info)
if plr.Character:FindFirstChild("Dual Uzis") then
local bullet1 = Instance.new("Part")
bullet1.Size = Vector3.new(0.08, 0.05, 1)
bullet1.Material = Enum.Material.Neon
bullet1.Color = Color3.fromRGB(255, 217, 0)
bullet1.Transparency = 0
bullet1.CanCollide = false
bullet1.Name = "Bullet"
bullet1.Position = Uzi.Barrel1.Position
bullet1.Orientation = plr.Character.HumanoidRootPart.Orientation
bullet.Parent = workspace
local velocity = Instance.new("BodyVelocity")
velocity.Velocity = hit.LookVector*300
end
end)
General comments:
- The syntax of the snippet of code you provide is incorrect since there is an extra
end at the last line
- You shouldn't use
Instance.new()s second parameter. An in-depth explanation of the same is on the DevForum.
I posted this answer since it can prove to be useful for anyone who stumbles upon this question in the future. Feel free to ask any questions or point out any mistakes!
5 comments
Is this a local script?
no
Player:GetMousereturns nil on the server, since the mouse instance is created locally. You can send certain properties of the mouse to the server, preferably viaRemoteEvents.How am I supposed to send properties of the mouse to the script if the script sees the mouse as nil
You can use a remote event and pass
Mouse.Hitor any other such required value directly e.g.: game.ReplicatedStorage.RemoteEvent:FireServer(mouse.Hit)