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

How can i make my pistol script work within Filtering Enabled?

Asked by 5 years ago

Greetings everybody, i'm wonder how i can use FE to my advantage on my pistol script so it works inside a fe game. Sadly, i do not have the knowledge to make a fe pistol. If its okay you can try help me a bit out.

~~ Server Script ~~

game.ReplicatedStorage.GunShot.OnServerEvent(function(remote)
    if remote == ("EntityShot") then
        print("e")
    end
end)

~~ Local Script inside pistol ~~

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

script.Parent.Equipped:Connect(function()
    mouse.Button1Down:Connect(function()
            if mouse.Target ~= nil then -- Check if it's not nil so checking its name won't return an error.
            if mouse.Target.Name == "Head" then -- Add .Name to get the instance's name
    print(mouse.Target.Parent)
    wait(.1)
    local entity = mouse.Target.Parent.Name
    game.ReplicatedStorage.MouseClick:FireServer("EntityShot",entity)
        local blood = script.Parent.Handle.ParticleEmitter:Clone().Parent == mouse.Target.Parent.Torso
    mouse.Target.Parent.Torso.Color = Color3.fromRGB(255,0,0)
    mouse.Target:Destroy()
                end
        end
    end)
end)

1 answer

Log in to vote
3
Answered by 5 years ago
Edited 5 years ago

There are a lot of small errors in this code all of which will cause your code to not work as expected and be inconsistent during the gameplay of this code.

Local script code


First looking at your local script you are connecting a new Button1Down event each time the tool is equipped. This means that you would get more than one gun shot per mouse press.

The Equipped event passes the player mouse. This is not the same as getting the mouse via the function GetMouse. The mouse passed in this event will disconnect all mouse events when unequipped.

For the most part the local script is ok but there is one big problem with the order of the code as you have included a wait/yeild in your code. This means that mouse.Target could change during this waiting time. To resolve this it is very common for programmers to make a variable of mouse.Target so that is will be the same even if there is a wait in your code.

You should not be blindly using Torso expecing it to be there but this can be easily solved by including some additional checks and using FindFirstChild to check if the child name exists.

Lastly you might want to include a debounce to prevent spam to the click event to make the gun seem more realistic with a delay.

Local script code

local remote = game:GetService("ReplicatedStorage"):WaitForChild("GunShot")
local blood = script.Parent:WaitForChild("Handle"):WaitForChild("ParticleEmitter")
local deb = false -- debounce

script.Parent.Equipped:Connect(function(mouse) -- arg mouse passed

    mouse.Button1Down:Connect(function()

        -- no target same as your nil check code
        if not mouse.Target or deb then return end 

        -- it is best to make a variable of mouse.Target
        -- just in case you add a wait/yield the code it can change
        local target = mouse.Target
        local torso = target.Parent:FindFirstChild("Torso")

        if target.Name == "Head" and torso then
            deb = true -- critical section start
            remote:FireServer("EntityShot", mouse.Target)

            blood:Clone().Parent = torso
            torso.Color = Color3.fromRGB(255,0,0)
            wait(0.1)

            deb = false -- critical section end
        end

    end)
end)

Server script code


Much like your local script there are a lot of small errors which will cause you problem in one way or another.

Firstly your remote event will not work as expected as you are firing game.ReplicatedStorage.MouseClick in the local script (this is fixed in the above code as well) and game.ReplicatedStorage.GunShot on the server.

This will then run your event however Roblox passe the player who fired the event as the first argument meaning your variable remote holds the player and not the arguments passed. In addition you have only included one argument but you are passing two arguments in the local script.

Lastly you should not be applying damage / deleting parts on the client side. These change will not replicate so these tasks need to be done on the server to work. Any effects will only be visible to that player as again it is local only and not done on the server.

Server script for remote event

game:GetService("ReplicatedStorage").GunShot.OnServerEvent(function(plr, txt, part)
    if txt == "EntityShot" then
    part:Destroy()
        print("e")
    end
end)

In this remote event code you will need to do sanity checks. Can the player shoot, do they have ammo, are they within range ect. Before performing any actions.

I hope this helps.

0
Can I just ask, if you keep your mouse down, and it's an automatic pistol, would he not then be correct? tonyv537 95 — 5y
0
Pistols aren't automatic User#24403 69 — 5y
0
There are machine/semi-automatic pistols. oftenz 367 — 5y
0
Well, it could be an automatic pistol yes. User#22722 20 — 5y
Ad

Answer this question