Here's the complete script setup for the problem:
LocalScript in StarterGui
This script creates the UI buttons and handles their click events by firing remote events to the server.
local player = game.Players.LocalPlayer
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "KnowledgeGui"
screenGui.Parent = player:WaitForChild("PlayerGui")
local button = Instance.new("TextButton")
button.Name = "KnowledgeButton"
button.Size = UDim2.new(0, 200, 0, 50)
button.Position = UDim2.new(0.5, -100, 0.5, -25)
button.Text = "Gain Knowledge"
button.Parent = screenGui
local sellButton = Instance.new("TextButton")
sellButton.Name = "SellButton"
sellButton.Size = UDim2.new(0, 200, 0, 50)
sellButton.Position = UDim2.new(0.5, -100, 0.6, -25)
sellButton.Text = "Sell Knowledge"
sellButton.Parent = screenGui
-- Now, handle the button clicks
local function onButtonClick()
game.ReplicatedStorage:WaitForChild("GainKnowledgeEvent"):FireServer()
end
button.MouseButton1Click:Connect(onButtonClick)
local function onSellButtonClick()
game.ReplicatedStorage:WaitForChild("SellKnowledgeEvent"):FireServer()
end
sellButton.MouseButton1Click:Connect(onSellButtonClick)
Script in ServerScriptService
This script handles the remote events fired by the LocalScript, managing the player's knowledge and coins, and enforcing the knowledge cap.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Create remote events
local gainKnowledgeEvent = Instance.new("RemoteEvent")
gainKnowledgeEvent.Name = "GainKnowledgeEvent"
gainKnowledgeEvent.Parent = ReplicatedStorage
local sellKnowledgeEvent = Instance.new("RemoteEvent")
sellKnowledgeEvent.Name = "SellKnowledgeEvent"
sellKnowledgeEvent.Parent = ReplicatedStorage
-- Function to handle gaining knowledge
local function onGainKnowledge(player)
-- Check if the player has a leaderstats folder
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local knowledge = Instance.new("IntValue")
knowledge.Name = "Knowledge"
knowledge.Value = 0
knowledge.Parent = leaderstats
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = leaderstats
-- Add a BoolValue to track if the player can gain knowledge
local canGainKnowledge = Instance.new("BoolValue")
canGainKnowledge.Name = "CanGainKnowledge"
canGainKnowledge.Value = true
canGainKnowledge.Parent = leaderstats
else
local knowledge = leaderstats:FindFirstChild("Knowledge")
if not knowledge then
knowledge = Instance.new("IntValue")
knowledge.Name = "Knowledge"
knowledge.Value = 0
knowledge.Parent = leaderstats
end
local coins = leaderstats:FindFirstChild("Coins")
if not coins then
coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = leaderstats
end
local canGainKnowledge = leaderstats:FindFirstChild("CanGainKnowledge")
if not canGainKnowledge then
canGainKnowledge = Instance.new("BoolValue")
canGainKnowledge.Name = "CanGainKnowledge"
canGainKnowledge.Value = true
canGainKnowledge.Parent = leaderstats
end
end
local knowledge = leaderstats:FindFirstChild("Knowledge")
local canGainKnowledge = leaderstats:FindFirstChild("CanGainKnowledge")
if canGainKnowledge.Value and knowledge.Value < 100 then
-- Calculate how much to add, ensuring we don't exceed 100
local newKnowledge = math.min(knowledge.Value + 5, 100)
knowledge.Value = newKnowledge
-- If we reached the cap, set canGainKnowledge to false
if newKnowledge >= 100 then
canGainKnowledge.Value = false
end
end
end
-- Function to handle selling knowledge
local function onSellKnowledge(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local knowledge = leaderstats:FindFirstChild("Knowledge")
local coins = leaderstats:FindFirstChild("Coins")
if knowledge and coins then
-- Sell all knowledge for coins (1:1 ratio)
coins.Value = coins.Value + knowledge.Value
knowledge.Value = 0
end
end
end
-- Connect the events
gainKnowledgeEvent.OnServerEvent:Connect(onGainKnowledge)
sellKnowledgeEvent.OnServerEvent:Connect(onSellKnowledge)
This setup allows players to gain knowledge by clicking a button, with a cap of 100 knowledge. Once the cap is reached, players cannot gain more knowledge until they buy from the shop (which is not implemented yet). Players can sell their knowledge for coins at any time, but this does not affect the knowledge cap. The knowledge cap is only lifted by purchasing items from the shop, which will be implemented later.