Welcome to the Forums!
Join thousands of other Roblox members with a free account.
Create Forum Account

How do I access this from a different script?

  • This website was created by Roblox members to replace the official forums (because Roblox shut them down) Join us if you like Roblox!

SuperjakeyLKR

New Member
Reputation: 0 / 0 / 0
Feb 19, 2021
1
0
1
Roblox
SuperjakeyLKR
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)
 

ElHomburgerGuy

If you won't let yourself fail, you won't improve
Reputation: 5 / 0 / 1
Apr 24, 2019
6,027
15
1,968
113
Anonymous in the USA
Roblox
PieroWasTaking
Discord
Piero#5261
Anything involving Screen GUI's (or GUIs in general I believe) must be in a LocalScript because GUIs affect player's individual screens.

If you wanna call a function in another Script/LocalScript then you'll need to do it in a ModuleScript. A simple example (because I myself and new to scripting as well 😅) would be.
ModuleScript:
local functions = {
    hello = function ()
       print("Hello, world!")
    end;
};


return functions;
and
LocalScript:
local functions = require(script.Parent.ModuleScript);

functions.hello()