Hello!
I am trying to create a purchasable door similar to those found in simulator games.
When the player touches the door, they should receive a GUI prompt asking if they want to purchase the door or not. The currency is a leaderstat value called “Tokens”, and the user should only be prompted if they have 3000 Tokens or more.
If the player chooses yes, they should have 3000 Tokens removed from their leaderstat, and the door should get destroyed. If they choose no, the GUI should just go away.
If the player were to leave and rejoin the game, the door should stay unlocked. I am pretty sure that I would need to use Datastores for this part, but I am not exactly sure how to do so. The elements and code that I currently have implemented is below:
There is a Part in Workplace named “Zone1Door”.
In ReplicatedStorage, there are two RemoteEvents named “OpenDoor1Gui” and “RemoveDoor1”.
In ServerScriptService, there is a Script that contains the following code:
local player = game.Players.LocalPlayer
local tokens = player.leaderstats.Tokens.Value
local openGui = game.ReplicatedStorage:WaitForChild("OpenDoor1Gui")
local door = game.Workspace:WaitForChild("Zone1Door")
local purchaseGui = game.StarterGui.Door1PurchaseGUI.Frame
game.Players.PlayerAdded:Connect(function(player)
--Function that checks if the player already bought the door
end)
door.Touched:Connect(function(player)
if tokens >= 3000 then
openGui:FireClient()
end
end)
Lastly, in StarterGui, there is a local script with the following code:
local event = game.ReplicatedStorage:WaitForChild("RemoveDoor1")
event.OnClientEvent:Connect(function()
local door = workspace.Zone1Door
door:Destroy()
end)
and a ScreenGui containing a frame with a “Yes” TextButton, a “No” TextButton, and a LocalScript that contains the following:
local player = game.Players.LocalPlayer
local tokens = player.leaderstats.Tokens.Value
local openGui = game.ReplicatedStorage:WaitForChild("OpenDoor1Gui")
local removeDoor = game.ReplicatedStorage:WaitForChild("RemoveDoor1")
local purchaseGui = script.Parent
local yesButton = script.Parent.Yes
local noButton = script.Parent.No
openGui.OnClientEvent:Connect(function()
purchaseGui.Visible = true
end)
yesButton.Activated:Connect(function()
tokens -= 3000
removeDoor:FireClient()
purchaseGui.Visible = false
end)
yesButton.Activated:Connect(function()
purchaseGui.Visible = false
end)
I am not too experienced in Roblox programming just yet, so anything helps, thank you!