Hello, so i made a coin system for my platformer. Basically, when the Server detects a coin being touched, it checks if there’s humanoid in it, and if there is, then it gets the player from the humanoid’s parent (character) and then it adds to the value of the leaderboard. On the Client, there’s all the fancy little effects like the Coin dissapearing and playing an animation and stuff. I made the exact same thing for my Crystal system (another collectible). The thing is, for both of them, some times when i test the game and go to collect coins or a crystal, they take a while to dissapear, and there is no effect or sound at all when they dissapear. Here’s a video :
And the code :
Client
--//Services//--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables//--
local Player = Players.LocalPlayer
local LeaderStats = Player:WaitForChild("leaderstats")
local CoinVal = LeaderStats.Coins
local PlayerGui = Player.PlayerGui
local MainGui = PlayerGui:WaitForChild("MainGui")
local Coins = MainGui.Coins
local CoinsFolder = workspace.Coins
local CoinDisplay = Coins.CoinDisplay
--//Function//--
for __, Coin in pairs(CoinsFolder:GetChildren()) do
local Sparkles = Coin:WaitForChild("Sparkles")
local Light = Coin:WaitForChild("PointLight")
local Collect = Coin:WaitForChild("Collect")
local function onTouched(Hit)
if Hit and Hit.Parent:FindFirstChild("Humanoid") then
Coin.CanTouch = false
Collect:Play()
Light:Destroy()
Sparkles.Enabled = true
Coin.Transparency = 1
Collect:Play()
Sparkles:Emit()
end
end
Coin.Touched:Connect(onTouched)
end
-----------------------------------------------------
while true do
CoinDisplay.Text = "x"..CoinVal.Value
task.wait()
end
-----------------------------------------------------
--//Services//--
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables//--
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:WaitForChild("Head")
local LeaderStats = Player:WaitForChild("leaderstats")
local CrystalVal = LeaderStats.Crystals
local PlayerGui = Player.PlayerGui
local MainGui = PlayerGui:WaitForChild("MainGui")
local Crystals = MainGui.Crystals
local CrystalDisplay = Crystals.CrystalDisplay
local CrystalsFolder = workspace.Crystals
local TweenInform = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
--//Function//--
for __, Crystal in pairs(CrystalsFolder:GetChildren()) do
local GreenSparkles = Crystal.GreenSparkles
local GreenGlow = Crystal.GreenGlow
local Light = Crystal.PointLight
local Glow = Crystal.Glow
local Collect = Crystal.Collect
local CSnd = Crystal.CSnd
local function onTouched(Hit)
if Hit and Hit.Parent:FindFirstChild("Humanoid") then
Glow:Stop()
Collect:Play()
CSnd:Play()
Crystal.CanTouch = false
Crystal.Transparency = 1
GreenSparkles:Destroy()
GreenGlow:Destroy()
Light:Destroy()
local GreenExplosion = Instance.new("Part", Root)
GreenExplosion.Size = Vector3.new(.5,.5,.5)
GreenExplosion.BrickColor = BrickColor.new(0, 255, 0)
GreenExplosion.Material = Enum.Material.Neon
GreenExplosion.Shape = Enum.PartType.Ball
GreenExplosion.Position = Root.Position
GreenExplosion.Transparency = 0
GreenExplosion.CanCollide = false
local Weld = Instance.new("WeldConstraint", GreenExplosion)
Weld.Part0 = GreenExplosion
Weld.Part1 = Root
local TweenInform = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local TweenGE = TweenService:Create(GreenExplosion, TweenInform, {Size = Vector3.new(6, 6, 6), Transparency = 1})
TweenGE:Play()
task.wait(.5)
GreenExplosion:Destroy()
end
end
Crystal.Touched:Connect(onTouched)
end
-----------------------------------------------------
while true do
CrystalDisplay.Text = "x"..CrystalVal.Value
task.wait()
end
-----------------------------------------------------
Server
--//Variables//--
local Players = game:GetService("Players")
--------------------------------------------
local CoinsFolder = workspace.Coins
local CoinTable = CoinsFolder:GetChildren()
local IBFolder = workspace.ItemBlocks
local IBTable = IBFolder:GetChildren()
local CrystalsFolder = workspace.Crystals
local CrystalTable = CrystalsFolder:GetChildren()
--//Function//--
local function onPlayerAdded(Player)
local function onCharacterAdded()
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local LeaderStats = Instance.new("Folder")
Humanoid.WalkSpeed = 26
Humanoid.JumpPower = 50
LeaderStats.Name = "leaderstats"
---------------------------
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = LeaderStats
---------------------------
local Crystals = Instance.new("IntValue")
Crystals.Name = "Crystals"
Crystals.Value = 0
Crystals.Parent = LeaderStats
---------------------------
LeaderStats.Parent = Player
end
Player.CharacterAdded:Connect(onCharacterAdded)
end
--------------------------------------------
for __, Coin in pairs(CoinTable) do
local function onCoinTouched(Hit)
if Hit and Hit.Parent:FindFirstChild("Humanoid") then
local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
---------------------------------------------------------
local Player = Players:GetPlayerFromCharacter(Humanoid.Parent)
---------------------------------------------------------
if Player == nil then
return
end
--------------------------------------------------------------------------------
local LeaderStats = Player:WaitForChild("leaderstats")
local CoinVal = LeaderStats:WaitForChild("Coins")
CoinVal.Value = CoinVal.Value + 1
task.wait(0.5)
Coin:Destroy()
end
end
Coin.Touched:Connect(onCoinTouched)
end
--------------------------------------------
for __, Block in pairs(IBTable) do
if Block:IsA("Model") and Block.Name == "ItemBlock" then
local Hitbox = Block.Hitbox
local HitS = Hitbox.HitS
local function onTouched(Hit)
if Hit and Hit.Parent:FindFirstChild("Humanoid") then
---------------------------------------------------------
local Player = Players:GetPlayerFromCharacter(Hit.Parent)
---------------------------------------------------------
if Player == nil then
return
end
--------------------------------------------------------------------------------
local LeaderStats = Player:WaitForChild("leaderstats")
local CoinVal = LeaderStats:WaitForChild("Coins")
CoinVal.Value = CoinVal.Value + 5
end
end
Hitbox.Touched:Once(onTouched)
end
end
--------------------------------------------
for __, Crystal in pairs(CrystalTable) do
local function onCrystalTouched(Hit)
if Hit and Hit.Parent:FindFirstChild("Humanoid") then
local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
---------------------------------------------------------
local Player = Players:GetPlayerFromCharacter(Humanoid.Parent)
---------------------------------------------------------
if Player == nil then
return
end
--------------------------------------------------------------------------------
local LeaderStats = Player:WaitForChild("leaderstats")
local CrystalVal = LeaderStats:WaitForChild("Crystals")
CrystalVal.Value = CrystalVal.Value + 1
task.wait(2.5)
Crystal:Destroy()
end
end
Crystal.Touched:Once(onCrystalTouched)
end
--------------------------------------------
Players.PlayerAdded:Connect(onPlayerAdded)