Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Where is my code error for firing a gun? The code errors and wont fire in FilteringEnabled

My code gives me an error saying "attempt to index local mouse (a nil value)" when I try to run it.

Here is the serverscriptservice server script.

local event = game.ReplicatedStorage.Events.ShotEvent
	

event.OnServerEvent:Connect(function(player)
	
	local mouse = thePlayer:GetMouse()
mouse.Icon = "rbxasset://textures\\GunCursor.png" 
local tool = player.Character.Uzi
    user = tool.Parent
    mouse.Button1Down:connect(function()
down = true
        --make and do a hit test along the ray
while down == true do
wait(0.0714285714)
game.Workspace.Shot:Play()

        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit*300)
        local hit, position = game.Workspace:FindPartOnRay(ray, user)

        --do damage to any humanoids hit
        local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid:TakeDamage(5)
        end

local distance = (position - tool.Barrel.CFrame.p).magnitude
        local rayPart = Instance.new("Part", user)
        rayPart.Name          = "RayPart"
        rayPart.BrickColor    = BrickColor.new("Bright red")
        rayPart.Transparency  = 0.5
        rayPart.Anchored      = true
        rayPart.CanCollide    = false
        rayPart.TopSurface    = Enum.SurfaceType.Smooth
        rayPart.BottomSurface = Enum.SurfaceType.Smooth
        rayPart.formFactor    = Enum.FormFactor.Custom
        rayPart.Size          = Vector3.new(0.01, 0.01, distance)
        rayPart.CFrame        = CFrame.new(position, tool.Barrel.CFrame.p) * CFrame.new(0, 0, -distance/2)

        --add it to debris so it disappears after 0.1 seconds
        game.Debris:AddItem(rayPart, 0.1)


mouse.Button1Up:connect(function()
down = false
end)
end
end)
end)

And the local code:

local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local user
local down = false
tool.Equipped:connect(function(mouse)
local event = game.ReplicatedStorage.Events.ShotEvent
event:FireServer()
end)


1 answer

The Player object passed to OnServerEvent is a Player object, not a Character; therefore, don't get the Player from the Player. Didn't read the rest of the code for errors.

5 comments

0

I fixed that but now I got line 7 attempt to index local mouse a nil value

0

thePlayer doesn't exist, so how can you get the mouse from thePlayer? player:GetMouse();

0

thats what I did, I fixed that first error and i did player:GetMouse() and it says it doesnt exist

0

Woops; you're trying to get the player's mouse in a localscript, which isn't permitted. You'll have to script all mouse-related functionality in the localscript, or maybe try passing the mouse from the localscript to the event (and therefore the server script)

0

*normal script isn't permitted

This discussion is preserved as it was. New answers, comments, and votes are closed.