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

Instancing 3 Bullets at once?

Asked by 2 years ago
Edited 2 years ago

my gun instances 3 bullets at once, but I put the line "instance" once? this might be a glitch.

gunEvent.OnServerEvent:Connect(function(plr, mouse)
    local par = RaycastParams.new()
    par.FilterDescendantsInstances = {plr.Character}
    par.FilterType = Enum.RaycastFilterType.Blacklist

    local ori = script.Parent.Handle.Position
    local direc = (mouse - ori) * 300
    local res = workspace:Raycast(ori, direc, par)

    if res and deb == true then
        deb = false
        local res2 = res.Instance
        local mod = res2:FindFirstAncestorOfClass("Model")
        local distance = (ori - res.Position).Magnitude
        local p = Instance.new("Part") 
        p.Color = Color3.fromRGB(255, 255, 127)
        p.Massless = true
        p.Anchored = true
        p.CanCollide = false
        p.Size = Vector3.new(0.1, 0.1, distance)
        p.CFrame = CFrame.lookAt(ori, res.Position)*CFrame.new(0, 0, -distance/2)
        p.Parent = workspace

I called instance once, but it spawned 3 bullets why? the rest of the script is about damaging humanoids. I even checked the whole script by doing find all, nothing. is there a problem? it used to spawn 2 bullets, now 3. ill summarize it for you. i put Instance.new("Part") once but it did it three times. i have a debounce to stop it from repeating. whats happening? i also checked CTRL + SHIFT + F and found nothing containing Instance.new("Part") except this line.

0
what does it look like Puppynniko 1059 — 2y

1 answer

Log in to vote
0
Answered by
Lakodex 711 Moderation Voter
2 years ago

The issue could actually be from the client. A recommendation is to have a value in the weapon to the firing rate in seconds, per bullet, so that you can make a cooldown via server.

local debounce = {}
local gunEvent = game.ReplicatedStorage.Shoot

gunEvent.OnServerEvent:Connect(function(plr, mouse)
    if not debounce[plr.UserId] then
        debounce[plr.UserId] = tick()
    end
    if debounce[plr.UserId] <= tick() then
        debounce[plr.UserId] = tick() + plr.Gun.FireRate.Value
        --fire
    end 
end)

The event will be disabled as long as the debounce is active. We're using tick() which get's the seconds that have passed since the 1st of January 1970. Adding our firerate seconds on will basically allow other users to use the event, and surpass their own cooldowns so it isn't every single user when one shoots.

Ad

Answer this question