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

RemoteEvent firing more than once when called?

Asked by 5 years ago

I have a LocalScript in which when a sound loops, it calls a RemoteEvent. (The part that calls the RemoteEvent is towards the bottom.) Here's that code:

repeat wait() until game.Players.LocalPlayer:FindFirstChild("PlayerGui")

game.Players.LocalPlayer.PlayerGui:SetTopbarTransparency(0)

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")
local background = script.Parent.background
local play = script.Parent.play
local picture = script.Parent.picture
local name = script.Parent.name
local me = script.Parent.me
local shop = script.Parent.ShopButton
local count = 0

play.MouseButton1Click:Connect(function()
    play:TweenPosition(UDim2.new(.375,0,1,0), "In", "Quad", 2)
    wait(1)
    name:TweenPosition(UDim2.new(1,0,.65,0), "In", "Quad", 2)
    wait(1)
    picture:TweenPosition(UDim2.new(0,0,-.6,0), "In", "Quad", 2)
    wait(1.5)
    repeat
        background.BackgroundTransparency = background.BackgroundTransparency + 0.01
        count = count + 1
        wait(0.03)
    until count == 100
    background:TweenPosition(UDim2.new(1,0,1,0), "In", "Quad", .5)
    wait(0.5)

    local sound = Instance.new("Sound", game.Players.LocalPlayer) --Creates new sound in player
    sound.Name = "oyy"
    sound.SoundId = "rbxassetid://2778989543"
    sound.Looped= true
    sound:Play() --Plays sound

    sound.DidLoop:Connect(function() --Checks if sound loops
        remote:FireServer() --If it does, execute this
    end)

end)

That part works fine I believe. Then, when the RemoteEvent is called it executes this code on the server:

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")

remote.OnServerEvent:Connect(function(player) --When RemoteEvent is called, execute this
    local yeahs = player:WaitForChild("leaderstats"):WaitForChild("yeahs") --Get player's stat
    yeahs.Value = yeahs.Value + 1 --Adds one to player's stat
end)

The problem is that it seems the RemoteEvent is sometimes called multiple times or doesn't get called at all because when I test this in game, it will usually add 2-3 yeahs to player and sometimes it doesn't do anything at all. Does anybody know what is happening or how to fix it?

0
I dont think using soundlooped is a good idea RetroGalacticGamer 331 — 5y
0
It's literally the only way that I know of to do what I want unless there's another way of tracking when a sound loops. ejones808 34 — 5y

1 answer

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

Okay, so basically your are using "MouseButton1Click" without a debounce. So, the click is detected multiple times, therefore running the script multiple times and running your event multiple times. Try this as your local script:

repeat wait() until game.Players.LocalPlayer:FindFirstChild("PlayerGui")

game.Players.LocalPlayer.PlayerGui:SetTopbarTransparency(0)

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")
local background = script.Parent.background
local play = script.Parent.play
local picture = script.Parent.picture
local name = script.Parent.name
local me = script.Parent.me
local shop = script.Parent.ShopButton
local count = 0
local clickdebounce = false

play.MouseButton1Click:Connect(function()
if clickdebounce == false then
clickdebounce = true
    play:TweenPosition(UDim2.new(.375,0,1,0), "In", "Quad", 2)
    wait(1)
    name:TweenPosition(UDim2.new(1,0,.65,0), "In", "Quad", 2)
    wait(1)
    picture:TweenPosition(UDim2.new(0,0,-.6,0), "In", "Quad", 2)
    wait(1.5)
    repeat
        background.BackgroundTransparency = background.BackgroundTransparency + 0.01
        count = count + 1
        wait(0.03)
    until count == 100
    background:TweenPosition(UDim2.new(1,0,1,0), "In", "Quad", .5)
    wait(0.5)

    local sound = Instance.new("Sound", game.Players.LocalPlayer) --Creates new sound in player
    sound.Name = "oyy"
    sound.SoundId = "rbxassetid://2778989543"
    sound.Looped= true
    sound:Play() --Plays sound

    sound.DidLoop:Connect(function() --Checks if sound loops
        remote:FireServer() --If it does, execute this
clickdebounce = false
    end)
end
end)
Ad

Answer this question