Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Gun script will not work for some reason? I'm using remote functions too!

Asked by
BashGuy10 379 Moderation Voter
1 year ago
Edited 1 year ago

I've solved this know, this answer is no longer in use.

LOCAL SCRIPT:

local tool = script.Parent
local config = tool:WaitForChild("Config")
local remotes = tool:WaitForChild("Remotes")
local actionservice = game:GetService("ContextActionService")
local equipped = false
local reloading = false
local lastShot = tick()

--Animations


--Sounds


--Remotes
local canreload = remotes:WaitForChild("CanReload")
local canshoot = remotes:WaitForChild("CanShoot")
local fire = remotes:WaitForChild("Fire")
local hit = remotes:WaitForChild("Hit")
local reload = remotes:WaitForChild("Reload")

--Config
local range = config:WaitForChild("Range")
local reloadtime = config:WaitForChild("ReloadTime")
local ammo = config:WaitForChild("Ammo")
local spareammo = config:WaitForChild("SpareAmmo")
local Damage = config:WaitForChild("Damage")
local headshot = config:WaitForChild("Headshot")
local magsize = config:WaitForChild("MagSize")
local firerate = config:WaitForChild("FireRate")

tool.Equipped:Connect(function()
    equipped = true
end)

tool.Unequipped:Connect(function()
    equipped = false
    reloading = false
end)

local function canshootf()
    print("Invoked!")
    if math.abs(lastShot - tick()) > (60/firerate.Value) then
        print("Gun fire has not went past the fire rate value")
        if equipped then
            print("Equipped is true")
            if not reloading then
                print("Reloading is false")
                if ammo.Value > 0 then
                    print("Ammo is greater than 1")
                    return true
                else
                    tool.Sounds.EmptySound:Play()
                end
            end
        end
    else
        print("Gun cannot shot")
        return false
    end

end

canshoot.OnServerInvoke = canshootf




oh well my local script part is gone for some reason

0
Also this is part of a bigger script, but that's not having problems. BashGuy10 379 — 1y
1
You're treating RemoteFunctions like a RemoteEvent, which they are not - https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events theking48989987 2122 — 1y

1 answer

Log in to vote
1
Answered by 1 year ago

My best guess is that on your "OnServerInvoke" function for the "canshoot" in the non-localscript side is that it will always return false therefore your localscript won't continue on the "MouseButton1Down" thing.

But other than that you might get better results if you put some "print" functions around to see if anything runs (such as putting it after an if statement) or to see values. Also showing us anything in the console with errors is something that will help us get to the end of your issue.

If you have any questions or issues contact me. :)

0
I get this error: "OnServerInvoke is a callback member of RemoteFunction; you can only set the callback value, get is not available" BashGuy10 379 — 1y
0
Alright, found out the problem. Apparently for remote functions you can't use something like "canshoot.OnServerInvoke:Connect(function()" BashGuy10 379 — 1y
0
And it was always returning false BashGuy10 379 — 1y
0
I know it's late; I haven't been here ages ago but your issue is quite easy to solve. It just means you've used it incorrectly. When you use a RemoteFunction, you need to treat it like a value. On the client side think of it like storing a value (value = game.ReplicatedStorage.RemoteFunction:InvokeServer(Data, somethingElse, etc). And on the server side treat it like your setting a value (...) lazycoolboy500 537 — 9mo
View all comments (3 more)
0
(game.ReplicatedStorage.OnServerInvoke = function() ... end lazycoolboy500 537 — 9mo
0
Just make sure your function returns a value because that is what it expects (something like "return wasShotted"). And then you can treat that value "Value" as if it were whatever it returned. lazycoolboy500 537 — 9mo
0
I know that you no longer have this script but this is just to solve the current issue if someone else has it. :) lazycoolboy500 537 — 9mo
Ad

Answer this question