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

why is the script causing spam even with a Debounce on it?

Asked by
lytew 99
4 years ago
Edited 4 years ago

hello. So, I created a script that adds "+1" to the money of the player who touched a coin. apparently, the script is right, but some coins that the player touches (I don’t know if it’s a glitch or something I forgot to put in the script), instead of adding "+1" to your money, a much larger number is added (+3 or +4 ...) script:

local player = game.Players.LocalPlayer
player.CharacterAdded:Wait()
db = false
local cointouchevent = game:GetService("ReplicatedStorage"):WaitForChild("Slotdata"):WaitForChild("CoinDestroy")
local hum = player.Character:WaitForChild("Humanoid")
pointsvalue = player:WaitForChild("leaderstats"):WaitForChild("Points")

    local sound = Instance.new("Sound",player.PlayerGui)
    sound.SoundId = 'rbxassetid://203620899'
    sound.MaxDistance = 100
    sound.Volume = 4


hum.Touched:Connect(function(hit)
            if hit.Name == "Coin" then
        if db == false then
    db = true
    print("more 1 money")   
    sound:Play()
    pointsvalue.Value = pointsvalue.Value + 1
hit:Destroy() 
db = false
end
end
end)

It is as if there was no Debounce (which I put to avoid spam.

1 answer

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

Hello. Try using a table. Try this:

local player = game.Players.LocalPlayer
player.CharacterAdded:Wait()
db = false
local cointouchevent = game:GetService("ReplicatedStorage"):WaitForChild("Slotdata"):WaitForChild("CoinDestroy")
local hum = player.Character:WaitForChild("Humanoid")
pointsvalue = player:WaitForChild("leaderstats"):WaitForChild("Points")

    local sound = Instance.new("Sound",player.PlayerGui)
    sound.SoundId = 'rbxassetid://203620899'
    sound.MaxDistance = 100
    sound.Volume = 4

local touchedCoins = {}

hum.Touched:Connect(function(hit)
            if hit.Name == "Coin" then
        if db == false then
    db = true

    for i, v in pairs(touchedCoins) do
          if v = hit then
        return nil
      end
    end

    table.insert(touchedCoins, 1, hit)
    print("Somar money")    
    sound:Play()
    pointsvalue.Value = pointsvalue.Value + 1
hit:Destroy() 
db = false
end
end
end)

coroutine.wrap(function()
    while wait(5) do
        for i = 1, #touchedCoins do
            table.remove(touchedCoins, i)
        end
    end
end)

When a coin is touched, it gets inserted into the table. Then every 5 seconds the coin in the touchedCoins array gets removed. Please accept and upvote this answer if it helped.

0
hello @ youtubermasterWOW. So, I tried, but if the wait is too long, when the player touches more than one currency, the value of your Money (Points) will not increase. lytew 99 — 4y
0
Try making the wait less longer. youtubemasterWOW 2741 — 4y
0
Actually, try now. I've edited it. youtubemasterWOW 2741 — 4y
0
Also, don't add the value on the client as it won't show for the server. Use a RemoteEvent. youtubemasterWOW 2741 — 4y
View all comments (8 more)
0
what's wrong with it not being displayed by the server? lytew 99 — 4y
0
If it's not displayed by the server, when your changing a value or checking if a player has coins, it'll return 0 as of Filtering Enabled. youtubemasterWOW 2741 — 4y
0
could I send this information to the server then? so that the player doesn't have a delay when picking up a coin lytew 99 — 4y
0
You'd also need a RemoteEvent for that. youtubemasterWOW 2741 — 4y
0
so i need to put a remoteevent to inform the server how many Points does the player have? lytew 99 — 4y
0
I guess you could use ":FireServer(player.leaderstats.Points.Value)" BUT that would be easily exploited. youtubemasterWOW 2741 — 4y
0
even with FilteringEnabled active? lytew 99 — 4y
0
Yes. Filtering Enabled is forced enabled now. youtubemasterWOW 2741 — 4y
Ad

Answer this question