The title basically summarizes my topic. In my coin script, whenever a coin is collected, the client fires a remote event to the server, and the server adds the coin to the leader stats (for security reasons, and because the coins are localized). The problem is if someone collects a row of coins too fast, the remote fires to much and adds 1-3 coins instead of 7. How would I fix this?
There are no errors, and this is controlled again by 2 scripts, a local script in StarterPlayerScripts, and a Script in ServerScriptService. I’m also using datastore 2. Another thing about the coin system is that whenever you collect a coin, you never collect it again.
Local Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coinManagerEvent = ReplicatedStorage.RemoteEvents:WaitForChild("coinManagerEvent")
local coins = {}
-- This is triggered when the player loads into the game. This hides the coins already collected.
coinManagerEvent.OnClientEvent:Connect(function(coinList)
local allCoins = game.Workspace.Coins:GetChildren()
for k,v in pairs(coinList) do
for k2,v2 in pairs(allCoins) do
if(v == v2.CoinID.Value) then
v2:Destroy()
table.remove(allCoins, k2)
end
end
end
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
3, -- Time (25 is default)
Enum.EasingStyle.Linear, -- EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
false, -- Reverses (tween will reverse once reaching it's goal)
0 -- DelayTime
)
local allCoins = game.Workspace.Coins:GetChildren()
for k,v in pairs(allCoins) do
local tween = TweenService:Create(v, tweenInfo, {CFrame = v.CFrame * CFrame.fromOrientation(0,0,math.rad(180))})
tween:Play()
local coinTouched = false
v.Touched:Connect(function(model)
local player = game.Players:GetPlayerFromCharacter(model.Parent)
if player == game.Players.LocalPlayer then
if not coinTouched then
coinTouched = true
ReplicatedStorage.RemoteEvents.coinGatherEvent:FireServer(v.CoinID.Value, player)
v:Destroy(player)
table.remove(allCoins,k)
game:GetService("SoundService").SoundEffects.Coin:Play()
wait(1)
coinTouched = false
end
end
end)
end
end)
ServerScript:
local serverScriptService = game:GetService("ServerScriptService")
local dataStore2 = require(serverScriptService.DataStore2)
local repStorage = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(player)
local CoinsGathered = dataStore2("CoinsGathered", player)
local coins = CoinsGathered:Get()
if(coins == nil) then
coins = {}
CoinsGathered:Set(coins)
end
repStorage:WaitForChild("RemoteEvents").coinManagerEvent:FireClient(player,coins)
end)
repStorage.RemoteEvents.coinGatherEvent.OnServerEvent:Connect(function(player, coinID)
local coin;
local coins = game.Workspace.Coins:GetChildren();
for k,v in pairs(coins) do
if(v.CoinID.Value == coinID) then
coin = v;
break;
end
end
if(coin == nil) then
print("Not a valid coin!");
else
print(player:DistanceFromCharacter(coin.Position))
if(player:DistanceFromCharacter(coin.Position) < 35.0) then
local CoinsGathered = dataStore2("CoinsGathered", player)
local temp = CoinsGathered:Get()
table.insert(temp, coinID)
CoinsGathered:Set(temp)
local coinStore = dataStore2("Coins", player);
if(coinStore:Get() == nil) then
coinStore:Set(0)
end
local lobbyCoins = dataStore2("LobbyCoins", player)
lobbyCoins:Increment(1, player)
coinStore:Increment(coin.CoinValue.Value, player)
else
print("Player isn't near the coin!")
end
end
end)
A debounce wouldn’t work because there are rows of coins going downward, so if you free fall only some of the coins would collect, which would not solve my problem. Should I just space out my coins more?
This topic was reposted. The previous post no one really understood the question, and I apologize since I worded it weirdly.
Have a good day!