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

How to fix a script that's firing an event for everyone?

Asked by 3 years ago

I don't know if the question is right. I don't really know what's happening.

It only lets one person do it at a time and it continues the script if the other didn't finish it. I thought it was firing the remote event for everyone and I tried printing the players name to see when I click the tool it prints both there names but it doesn't, so I thought this couldn't be it.

https://gyazo.com/f238d52ed106d58c4eacb8601d279c2b https://gyazo.com/ca3f1ee72a66fc7e6efe50fcbdb31e73

local script


local tool = script.Parent local player = game.Players.LocalPlayer local debounce = false local debounce2 = false tool.Equipped:Connect(function() game.ReplicatedStorage.Events.M1CombatEvents.CombatIdleOn:FireServer() end) tool.Unequipped:Connect(function() game.ReplicatedStorage.Events.M1CombatEvents.CombatIdleOff:FireServer() end) tool.Activated:Connect(function() if player.Character and tool:IsDescendantOf(player.Character) then if player and player == game.Players.LocalPlayer and debounce == false then game.ReplicatedStorage.Events.M1CombatEvents.M1Event:FireServer() debounce = true wait(1) debounce = false end end end) tool.Activated:Connect(function() if player.Character and tool:IsDescendantOf(player.Character) then if player and player == game.Players.LocalPlayer and debounce2 == false then game.ReplicatedStorage.Events.M1CombatEvents.M1Event2:FireServer() debounce2 = true wait(1) debounce2 = false end end end)

server script

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://7204285119"
local PlayAnim

local anim2 = Instance.new("Animation")
anim2.AnimationId = "rbxassetid://7424052691"
local PlayAnim2

local anim3 = Instance.new("Animation")
anim3.AnimationId = "rbxassetid://7424918789"
local PlayAnim3




game.ReplicatedStorage.Events.M1CombatEvents.CombatIdleOn.OnServerEvent:Connect(function(player)
    local hum = player.Character.Humanoid
    PlayAnim3 = hum:LoadAnimation(anim3)
    PlayAnim3:Play()
end)

game.ReplicatedStorage.Events.M1CombatEvents.CombatIdleOff.OnServerEvent:Connect(function(player)
    PlayAnim3:Stop()
end)

local debounce = false
local debounce2 = false
local doDamage = true
local doDamage2 = true

game.ReplicatedStorage.Events.M1CombatEvents.M1Event.OnServerEvent:Connect(function(player)

        local char = player.Character
        local hum = player.Character.Humanoid
        PlayAnim = hum:LoadAnimation(anim)

        if debounce == false then
            PlayAnim:Play()

            local rightarm = char["Right Arm"]

            rightarm.Touched:Connect(function(Hit)
                if not Hit:IsDescendantOf(player.Character) then
                    if Hit:IsA("Part") or Hit:IsA("MeshPart") then
                        local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
                        if Humanoid and doDamage == true and PlayAnim.IsPlaying then
                            Humanoid:TakeDamage(10)
                            doDamage = false
                        end
                    end
                end
            end)
            debounce = true
            wait(2)
            doDamage = true
            debounce = false

    end
end)

game.ReplicatedStorage.Events.M1CombatEvents.M1Event2.OnServerEvent:Connect(function(player)
        local char = player.Character
        local hum = player.Character.Humanoid
        PlayAnim2 = hum:LoadAnimation(anim2)

        if debounce == true and debounce2 == false and PlayAnim.IsPlaying == false then
            PlayAnim2:Play()

            local leftarm = char["Left Arm"]

            leftarm.Touched:Connect(function(Hit)
                if not Hit:IsDescendantOf(player.Character) then
                    if Hit:IsA("Part") or Hit:IsA("MeshPart") then
                        local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
                        if Humanoid and doDamage2 == true and PlayAnim2.IsPlaying then
                            Humanoid:TakeDamage(10)
                            doDamage2 = false
                        end
                    end
                end
            end)
            debounce2 = true
            wait(2)
            doDamage2 = true
            debounce2 = false

    end 
end)



0
Not sure what the problem is.. what is supposed to happen? what is happening that shouldn't? Also, you should load animations from the client, unless the server has network ownership of that character (like an NPC), and if you can you should use the Animator:LoadAnimation() function in new scripts. https://developer.roblox.com/en-us/api-reference/function/Animator/LoadAnimation LeHelary 142 — 3y
0
The problem is that when one person clicks on the tool it counts as activating for everyone else, so you cant click with the tool cause it activated the cooldown. Also, wouldn't loading animations from the client make it so other people cant see it or is that only when playing the animation from the client. 360gamer107 6 — 3y
0
When you play animations on the client's character, from the client itself (LocalScript), all other players see that animation playing because each player has control of its own character (network ownership). About the problem with cooldowns, i'm gonna see and then let you know LeHelary 142 — 3y
0
Ah, I see what you mean now with the animations. 360gamer107 6 — 3y

1 answer

Log in to vote
0
Answered by
LeHelary 142
3 years ago

So, as I said in the comment you should probably review the code and play animations from the client. That being said, after some "cleaning" of the script, here's how to fix the debounce problem. Instead of using a simple boolean, that would block execution for everyone, you could use an array of player IDs. For example, with a single debounce value:

local debounce = {}

-- to check if player has debounce active
if table.find(debounce, plr.UserId) then -- will execute if debounce is active, use 'not' for inverse

-- to enable debounce for a player
table.insert(debounce, plr.UserId)

-- to disable debounce for a player
table.remove(debounce, table.find(debounce, plr.UserId))
0
thanks huge help 360gamer107 6 — 3y
Ad

Answer this question