I have two separate scripts and one of them creates a new value called 'gold'. I need to make it so when someone clicks a text button they get gold. But I can't figure out how to link the two scripts together to use the gold value in another script.
The script that creates the gold value:
local function onPlayerJoin(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local gold = Instance.new("IntValue") gold.Name = "Gold" gold.Value = 0 gold.Parent = leaderstats end game.Players.PlayerAdded:Connect(onPlayerJoin)
The script that is meant to give gold:
game.StarterGui.ScreenGui.Frame.TextButton.MouseButton1Click:Connect(function(player) gold.Value = gold.Value + 1 end)
First thing, please write your code in a code block, so it can be understood easily.
Second, the contents in StarterGui get copied to PlayerGui. So, do not use StarterGui.
Third, you can insert a LocalScript inside the textbutton and can use:
script.Parent.MouseButton1Click:Connect(function()
And then you can fire an remote event:
game.ReplicatedStorage.YOUR_REMOTE_EVENT_NAME:FireServer()
Make sure you have the remote event in Replicated Storage and rename it to the name that you can gonna use
Then: You can insert a script in ServerScriptService:
game.ReplicatedStorage.YOUR_REMOTE_EVENT_NAME.OnServerEvent:Connect(function(player) player:WaitForChild("leaderstats").Gold.Value = player:WaitForChild("leaderstats").Gold.Value + 1 end)
You can add debounce if you want.
Why am I using Local Script? Because it will reduce server lag, although if you are using it for clicking it again and again, then it will result in server lag.
And a quick tip: Don't use normal/server scripts for manipulating UI elements until extremely necessary.
Lemme know if it helps!
In my perspective, it'd be better to get the gold values and edit them from the server using Remotes. In this case, I'll use RemoteFunction
in case you want anything else to be processed there. If you want this to be synchronous, use RemoteEvent
instead. BestCreativeBoy knows what's up!
Read this article for more info on how to use these.
I'll name that remote 'GiveGold'. It gives people gold off your wallet by sending a request to the server and gain access to the leaderstats, then increases it on the server.
This RemoteFunction
is placed in ReplicatedStorage
in this case. You can move it anywhere, but remember to fix the scripts so it'll acknowledge where it is.
The LocalScript
that gives gold (placed inside the button that gives gold):
local textbutton = script.Parent local rep = game.ReplicatedStorage --Both clients and server sees this --The gold will not increase due to the cooldown. It will only increase when the cooldown is over! textbutton.MouseButton1Click:Connect(function() rep.GiveGold:InvokeServer() --This will tell the server to give you gold end)
This will handle the request sent from clients that invoked it. This should be done with a Server script (aka Script), and should be placed anywhere the script runs like ServerScriptService.
local rep = game.ReplicatedService --See, I can see this too local players = game.Players local run = game:GetService('RunService') --RunService for cooldown updates local MAX_COOLDOWN = 1 --a second --This is a table of players with their respective cooldown values. When the player client invokes the remote, their cooldown value will be set to the MAX_COOLDOWN value. If the client invokes the remote again while their cooldown is not 0, the request will be denied and the player will not receive their gold until their cooldown finishes. local cooldowns = {} --Add players to the cooldown list with a default value of zero when they joined. players.PlayerAdded:Connect(function(plr) --something up here cooldowns[plr] = 0 --something down here end) --Remove them from player list when they leaves. players.PlayerRemoving:Connect(function(plr) --something up here cooldowns[plr] = nil --Consider them being wiped off their existence --something down 'ere end) --Cooldown managing run.Heartbeat:Connect(function(dt) --dt is delta time - well... just simply understand this as the number of seconds passed between physics frames. --We will use this to deter player's cooldown. for plr, seconds in pairs(cooldowns) do --'seconds' is the remaining cooldown of a player this loop session has gotten. if seconds > 0 then --The cooldown is still up. Subtract it. cooldowns[plr] = cooldowns[plrs] - dt end end end) --Create a function that will give gold to the player. local function onGoldRequest(plr) --plr is Player, which by default be passed when client invokes/fires to the server as the first argument. if not (cooldowns[plr] and cooldowns[plr] <= 0 and players:FindFirstChild(plr.Name)) then --If their cooldown is unfinished or the player does not exist, don't increase the gold amount. (Just adding player check to make sure everything's good) return end --Set their cooldown. cooldowns[plr] = MAX_COOLDOWN local gold = plr.leaderstats.gold --Get the 'gold' number/int value gold.Value = gold.Value + 1 --Gives 1 gold. end --Bind the function to the RemoteFunction. If you're using RemoteEvent, use :Connect(function) instead. rep.GiveGold.OnServerInvoke = onGoldRequest --This will pass the function as a memory object, not a function call - which means it will not run when it is passed without brackets, so you can do like 'local giveGold = onGoldRequest' and call giveGold() which will basically do the same as onGoldRequest(). Give it a try if you want
In the second script, you would have to have not just
gold.value
but
game.Players.leaderstats.gold.Value
Hope this helps!