So i have recently made this system, with coins that add up to tally on leaderboard and i’m the only one who can spawn them.
My question would be, how can this code be improved and what general tips would there be.
I had help with only one part of this system, which would be visibility of menu that opens the coin spawn
All visibiliy scripts are LocalScripts, while everything else is in usual script.
Credit to devforum member for GUI visibility script: Awesom3_Eric
GUI visibility script:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local button = script.Parent
if player.UserId ~= game.CreatorId then
button:Destroy()
end
Visibility script for coin spawner buttons:
local visible = false
local activator = script.Parent.Parent.OpenButton
local button = script.Parent
local function buttonActivation()
if (visible == false) then
button.Visible = false
elseif (visible ~= false) then
button.Visible = true
end
if activator.MouseButton1Click and (visible == false) then
visible = true
elseif activator.MouseButton1Click then
visible = false
end
end
activator.MouseButton1Click:Connect(buttonActivation)
Script for actual coin spawning:
local button = script.Parent
local coin = game.ReplicatedStorage.Coins.SilverCoin
local function buttonActivated()
if button.MouseButton1Click then
local ClonedCoin = coin:Clone()
ClonedCoin.Parent = game.Workspace
ClonedCoin:SetPrimaryPartCFrame(CFrame.new(0, 50, 0))
end
end
button.MouseButton1Click:Connect(buttonActivated)
Script for the coins themselves:
local Players = game:GetService("Players")
local coin = script.Parent
local function onPartTouch(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
coin:Destroy()
local player = Players:GetPlayerFromCharacter(partParent)
local leaderstats = player.leaderstats
local coinsStat = leaderstats and leaderstats:FindFirstChild("Coins")
if coinsStat then
coinsStat.Value = coinsStat.Value + 10
end
end
end
coin.Touched:Connect(onPartTouch)