I am relatively new to ROBLOX development and LUA, so I was confused on how to do this. I want to click an ImageButton and for it to update the leaderboard. The "clicks" variable doesn't work unless it is local.
First script (In ServerScriptService):
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local clicks = Instance.new("IntValue")
clicks.Name = "Clicks"
clicks.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserID.."-clicks")
end)
if success then
clicks.Value = data
print("Player Data loaded successfully.")
else
print("There was an error loading the Player Save Data.")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserID.."-clicks", player.leaderstats.Click.Value)
end)
if success then
print ("Player Data saved successfully.")
else
print ("There was an error saving the Player Save Data")
warn(errormessage)
end
end)
Second script (In the ScreenGUI for the ImageButton):
local buttonstart = script.Parent.ImageButton
local function onButtonActivated ()
local currentvalue = script.Parent.Parent.ScreenGui.Frame.Counter.Text
script.Parent.Parent.ScreenGui.Frame.Counter.Text = currentvalue + 1
end
buttonstart.Activated:Connect(onButtonActivated)