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

How to get the mouse with filtering enabled on? (In a gun script)

Asked by 6 years ago

When I run the code locally it works well but when I run it on a server, it does not recognize the mouse. I am pretty new to filtering enabled and I need help. Side note: the damage does not work either.

Local script:

h = script.Parent.Handle


script.Parent.Activated:Connect(function()
    game.ReplicatedStorage.RemoteEvent:FireServer(h)
end)

Server script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr,  handle)
    local m = plr:GetMouse()
    local b = Instance.new("Part",game.Workspace)
    b.Shape = "Ball"
    b.BrickColor = BrickColor.new("Really red")
    b.Size = Vector3.new(1,1,1)
    b.CFrame = CFrame.new(handle.Position, mouse.hit.p)
    local v = Instance.new("BodyVelocity",b)
    v.Velocity = b.CFrame.lookVector * 400
    v.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
    local d = game:GetService("Debris")
    d:AddItem(b, 5)
    b.Touched:connect(function(hit)
        if hit ~= handle then       
            local h = hit.Parent:FindFirstChild("Humanoid")
            if h then
                if h.Parent.Name ~= plr.Name then       
                h:TakeDamage(10)
                end
            end
        end
    end)
end)

2 answers

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

The problem is you're trying to get the mouse from a server script. This will throw an error. You can pass things like the Hit and Target as parameters, though.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("RemoteEvent")
Remote.OnServerEvent:Connect(function(plr,handle,hit)
    local b = Instance.new("Part") -- parent argument is deprecated and slows down game performance, assign in another line
    b.Shape = Enum.PartType.Ball -- assign as enums, makes it faster
    b.BrickColor = BrickColor.new("Really red")
    b.Size = Vector3.new(1,1,1)
    b.CFrame = CFrame.new(handle.Position, hit.p)
    b.Parent = game.Workspace -- the parent should always be the last thing to assign

    local dmgScript = script.DmgScript:Clone() -- the touched event script is a separate script
    dmgScript.Parent = b
    dmgScript.Disabled = false -- manually set disabled to true first, we then enable it

    local v = Instance.new("BodyVelocity")
    v.Velocity = b.CFrame.lookVector * 400
    v.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
    v.Parent = b
    local d = game:GetService("Debris")
    d:AddItem(b, 5)
end)

The DmgScript

script.Parent.Touched:Connect(function(hit) -- :connect is deprecated, switch to :Connect
    if hit.Name == "Handle" then return end -- if a handle, stop function

        local h = hit.Parent:FindFirstChild("Humanoid")

        if h then
            h:TakeDamage(10)
        end
    end
end)

The local script:

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local h = script.Parent.Handle


script.Parent.Activated:Connect(function()
    game.ReplicatedStorage.RemoteEvent:FireServer(h, mouse.Hit) -- Hit not hit
end)
0
Thank you very much. The extra tips have also helped me out a lot. I appreciate it. GodOfRBLX 21 — 6y
Ad
Log in to vote
1
Answered by 6 years ago

I would suggest passing the mouse from the client alongside everything else. To do that, just create another argument in your server script.

I'm calling it "m" since that's what you call the mouse in your script previously

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr,  handle, m)

& pass it along from the client: (localscript)

local lp = game.Players.LocalPlayer
 game.ReplicatedStorage.RemoteEvent:FireServer(h, lp:GetMouse())

Delete the "local m = plr:GetMouse()" line from your server script.

Your final LocalScript code should look like this:

local h = script.Parent.Handle
local lp = game.Players.LocalPlayer

script.Parent.Activated:Connect(function()
    game.ReplicatedStorage.RemoteEvent:FireServer(h, lp:GetMouse())
end)

And your serverscript code should look like this:


game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, handle, m) -- we no longer need to use GetMouse(), since it's already passed local b = Instance.new("Part",game.Workspace) b.Shape = "Ball" b.BrickColor = BrickColor.new("Really red") b.Size = Vector3.new(1,1,1) b.CFrame = CFrame.new(handle.Position, mouse.hit.p) local v = Instance.new("BodyVelocity",b) v.Velocity = b.CFrame.lookVector * 400 v.MaxForce = Vector3.new(math.huge,math.huge,math.huge) local d = game:GetService("Debris") d:AddItem(b, 5) b.Touched:connect(function(hit) if hit ~= handle then local h = hit.Parent:FindFirstChild("Humanoid") if h then if h.Parent.Name ~= plr.Name then h:TakeDamage(10) end end end end) end)

Note: I made it pretty copy-paste friendly, and I'm sorry for that. Please read over what I've said and not just copy and paste if it does end up working.

0
No, passing the mouse object itself is the same as calling GetMouse on the server. User#19524 175 — 6y
0
True I tried that already. But thanks anyway. GodOfRBLX 21 — 6y
0
Oh damn it. I was on the right track but I gave a wrong answer. I thought I knew this stuff already... ReallyExpensive 10 — 6y

Answer this question