So I tried to make a gun, and I I want to make it so that the bullet shoots in the direction of your mouse, but it doesn't want to detect the mouse. This is a script located in the StarterPack.
local p = script.Parent.Parent print(p.Name) local Uzi = script.Parent["Dual Uzis"] Uzi.Activated:Connect(function() local mouse = p:GetMouse() local bullet1 = Instance.new("Part",workspace) 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" Instance.new("BodyVelocity",bullet1) bullet1.Position = Uzi.Barrel1.Position bullet1.Orientation =p.Character.HumanoidRootPart.Orientation bullet1.BodyVelocity.Velocity = mouse.Hit.p.LookVector*300 end) end
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 viaRemoteEvent
s.
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 ```lua local remote = game:GetService("ReplicatedStorage"):WaitForChild("Remote", 10)
remote.OnServerEvent:Connect(function(plr, hit) -- hit is the Mouse.Hit CFrame
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: ```lua 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) ```
Another option, one that can be used if multiple tools use the same remote is to change the server script to this: ```lua local remote = game:GetService("ReplicatedStorage"):WaitForChild("Remote", 10)
remote.OnServerEvent:Connect(function(plr, info) -- info can vary for each tool
if plr.Character:FindFirstChild("Dual Uzis") then
-- Dual Uzis is equipped
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!
You need to divide it into two scripts, one needs to be local and one regular. Put the local script in the place of your current one and put a regular one inside an event which can be let's say in the workspace. You can name the event "UziEvent"
Have this in the local script:
local p = script.Parent.Parent local event = workspace.UziEvent print(p.Name) local Uzi = script.Parent["Dual Uzis"] Uzi.Activated:Connect(function() local mouse = p:GetMouse() event:FireServer(mouse.Hit,Uzi) end)
And have this in the regular script:
wait() local event = workspace.UziEvent event.OnServerEvent:Connect(function(p,mouse.Hit,Uzi) local bullet1 = Instance.new("Part",workspace) 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" Instance.new("BodyVelocity",bullet1) bullet1.Position = Uzi.Barrel1.Position bullet1.Orientation =p.Character.HumanoidRootPart.Orientation bullet1.BodyVelocity.Velocity = mouse.Hit.p.LookVector*300 end)
This should technically work. Let me know if it outputs an error anywhere. Hope this helps.