Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

roblox attemp to index global 'mouse' a nil value? (filtering enabled gun)

Asked by 6 years ago

im trying to make a gun on filtering enabled on, the remote event is in replicatedstorage. here's the script:

script.Parent.Parent.Activated:connect(function() game.ReplicatedStorage.GunShootingEvent:FireServer() end)

then here's the script for the main content

game.ReplicatedStorage.GunShootingEvent.OnServerEvent:connect(function(player) part = Instance.new("Part",game.Workspace) mouse = player:GetMouse() game.Debris:AddItem(part, 2) part.Size = Vector3.new(0.2,0.2,3) part.Material = "Neon" part.TopSurface = "Smooth" part.BottomSurface = "Smooth" part.LeftSurface = "Smooth" part.RightSurface = "Smooth" part.BrickColor = BrickColor.new("New Yeller") part.CanCollide = false part.CFrame = player.Character.StarterPistol.Handle.CFrame part.CFrame = CFrame.new(part.Position, mouse.Hit.p) v = Instance.new("BodyVelocity",part) v.Velocity = part.CFrame.lookVector * 500 v.MaxForce = Vector3.new(math.huge,math.huge,math.huge)

end)

please I really need help on this, i put a variable stating the mouse, and I have no clue why it doesnt work.

0
Wrap your code into a code block and cut out everything that isn't part of your problem so it's easier to read. Otherwise, there's a high chance that NO ONE will give you quality answers. laughablehaha 494 — 6y
0
Also, your RemoteEvent for firing a gun should be placed inside of the actual tool instead of inside of ReplicatedStorage. laughablehaha 494 — 6y
0
i did it, but it still wouldnt work. FaZe_McDouble -7 — 6y
0
You cannot get the mouse from the server, and the parent argument to Instance.new is deprecated User#19524 175 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This is happening because GetMouse can only be called on the client. If you need to get the Hit of the mouse, pass it as a parameter, and when you call FireServer from a client, pass the Hit as an argument.

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

script.Parent.Parent.Activated:Connect(function() -- connect is deprecated, use Connect
    game.ReplicatedStorage.GunShootingEvent:FireServer(mouse.Hit) -- sending Hit as arg
end)

Server:

game.ReplicatedStorage.GunShootingEvent.OnServerEvent:Connect(function(player, hit) 
    local part = Instance.new("Part") -- use more local variables, and the parent argument is deprecated and can cause performance issues 
    part.Size = Vector3.new(0.2,0.2,3) 
    part.Material = Enum.Material.Neon -- use enums 
    part.BrickColor = BrickColor.new("New Yeller") 
    part.CanCollide = false 
    part.CFrame = CFrame.new(part.Position, hit.p) 
    part.Parent = workspace -- always parent last 
    game:GetService("Debris"):AddItem(part, 2)
    local v = Instance.new("BodyVelocity") 
    v.Velocity = part.CFrame.lookVector * 500 
    v.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
    v.Parent = part
end)

On a side note, post your code in code blocks next time, like this:

https://gyazo.com/4b80f7bfcd023035290e5498e2cb28e7

Ad

Answer this question