#linking datastore to purchase event issue
171 messages · Page 1 of 1 (latest)
this is the tutorial I followed to make the datastore and coin collection system, and it works perfectly.
but im trying to link a purchase event in a shop to take away from that data
Put the purchase event in the same script as the datastore, it will allow you to manipulate the data without excessive coding
** You are now Level 1! **
yeah I tried that but it didnt work
I just added it to the bottom of datastoreservice
actually I got confused im trying to link it to my already existing leaderstats value
the datastore is fine I was just confused
is this okay though? I just added the purchasevent script to the bottom of datastore
That would be fine even if it's separate or not, I only do that when I need to manipulate something in that script or I'm too lazy to visit separate stuffs when I can just put them j. One
okay but how do I get the shop script to subtract and actually give me the item
** You are now Level 2! **
I dont understand why it isnt working
local PurchaseEvent = ReplicatedStorage:WaitForChild("PurchaseEvent")
local Weapons = ReplicatedStorage:WaitForChild("Weapons")
PurchaseEvent.OnServerEvent:Connect(function(player, weaponName)
local findinfo = Weapons:FindFirstChild(weaponName)
if findinfo then
local price = findinfo:FindFirstChild("Price")
local tool = findinfo:FindFirstChild("Tool")
if price and tool then
if player.leaderstats.Parasites.Value >= price.Value then
player.leaderstats.Parasites.Value = player.leaderstats.Parasites.Value - price.Value
local clonedtool = tool:Clone()
clonedtool.Parent = player.Backpack
end
end
end
end)```
Can you do some debugging and show me the errors
Datastore can't be accessed on client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage:WaitForChild("PurchaseEvent")
local Main = script.Parent
local Purchaseables = Main:WaitForChild("Purchaseables")
local DataStoreService = game:GetService("DataStoreService")
local currencyDataStore = DataStoreService:GetDataStore("Parasites")
for i, weapon in pairs(Purchaseables:GetChildren()) do
weapon.MouseButton1Click:Connect(function()
local success, currency = pcall(function()
return currencyDataStore:GetAsync(Player.UserId)
end)
if success then
local weaponName = weapon.Name
local weaponPrice = 10 -- Set the price of the weapon here
if currency >= weaponPrice then
currencyDataStore:SetAsync(Player.UserId, currency - weaponPrice)
PurchaseEvent:FireServer(weaponName)
else
warn("Not enough currency to purchase " .. weaponName)
end
else
warn("Error fetching currency data")
end
end)
end```
the purchaseableshandler script
OHHH
The datastore might be what's breaking your script
They can't be accessed on the client to prevent exploits from deleting their data
yeah that makes sense
Simply making some remote functions in ReplicatedStorage
No
Lemme explain for a bit
The datastore is only accessible on the server
So
You can make a Remote "Function" event on the server script
The datastore handler
That will return the data after grabbing it
I dont understand that
Yeah datastore is quite complicated
my datastore works fine but the issue is tryna subtract from it when I buy a weapon
from leaderstats
I've been going in circles sorry
Just delete some datastore related stuff on the client script
It will prob work
what do I replace it with
All of the stuffs I said before
Need me to explain it again in a more human way?
And not an AI?
yeah im new to this
** You are now Level 2! **
theres no global value named currencyDataStore
idk what to set it as
if I cant use datastore service on client
Screenshot
Delete that too
the entire line?
You have your coins and stuff in leaderstats right?
If yes, let the server handle the data stuff
You simply grab the coins from the player's leaderstats
yes my coins when I pick them up save and work fine
Cool
There's no currency there
and a variable for weapon price
Have you learned about how to grab the player that the script runs for?
this is it right
Prob
** You are now Level 3! **
ohhhh on currency
take your time
the datastore script?
the part where it handles the purchase yeah
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage:WaitForChild("PurchaseEvent")
local Main = script.Parent
local Purchaseables = Main:WaitForChild("Purchaseables")
local Currency = Player:WaitForChild("leaderstats"):WaitForChild("Parasites")
local DataStoreService = game:GetService("DataStoreService")
for i, weapon in pairs(Purchaseables:GetChildren()) do
weapon.MouseButton1Click:Connect(function()
local success, currency = pcall(function()
end)
if success then
local weaponName = weapon.Name
local weaponPrice = 10 -- Set the price of the weapon here
if Currency.Value >= weaponPrice then
PurchaseEvent:FireServer(weaponName)
else
warn("Not enough currency to purchase " .. weaponName)
end
else
warn("Error fetching currency data")
end
end)
end```
mb i meant the datastore
its too long
i saw that
so
you can add a few print lines
before and after some events
to check which part is wrong
in the purchase handler script right
the datastore script?
oh at the bottom
local PurchaseEvent = ReplicatedStorage:WaitForChild("PurchaseEvent")
local Weapons = ReplicatedStorage:WaitForChild("Weapons")
PurchaseEvent.OnServerEvent:Connect(function(player, weaponName)
local findinfo = Weapons:FindFirstChild(weaponName)
if findinfo then
local price = findinfo:FindFirstChild("Price")
local tool = findinfo:FindFirstChild("Tool")
if price and tool then
if player.leaderstats.Parasites.Value >= price.Value then
player.leaderstats.Parasites.Value = player.leaderstats.Parasites.Value - price.Value
local clonedtool = tool:Clone()
clonedtool.Parent = player.Backpack
end
end
end
end)```
in this part
can u give me an example of how to use print to check if it works?
can I just do it like this every few lines?
wouldnt I need an if statement?
that's what i meant
after crucial parts
alr
** You are now Level 3! **
how about the if price and tool
local PurchaseEvent = ReplicatedStorage:WaitForChild("PurchaseEvent")
local Weapons = ReplicatedStorage:WaitForChild("Weapons")
PurchaseEvent.OnServerEvent:Connect(function(player, weaponName)
local findinfo = Weapons:FindFirstChild(weaponName)
print("findinfo worked")
if findinfo then
local price = findinfo:FindFirstChild("Price")
local tool = findinfo:FindFirstChild("Tool")
print ("Found price and tool")
if price and tool then
if player.leaderstats.Parasites.Value >= price.Value then
player.leaderstats.Parasites.Value = player.leaderstats.Parasites.Value - price.Value
print("Leaderstats Changed")
local clonedtool = tool:Clone()
clonedtool.Parent = player.Backpack
print("Tool Added")
end
end
end
end)```
so like this
yes
alr lemme run it
coding is just trial and errors really
OHH
bro that helps so much
I needa start using print more
the issue is in this
wait but that wont work because price is a local script right?
** You are now Level 4! **
local price = ReplicatedStorage.Weapons.Sword.Price
local tool = ReplicatedStorage.Weapons.Sword.Sword
print ("Found price and tool")
if price and tool then
if player.leaderstats.Parasites.Value >= price.Value then
player.leaderstats.Parasites.Value = player.leaderstats.Parasites.Value - price.Value
print("Leaderstats Changed")
local clonedtool = tool:Clone()
clonedtool.Parent = player.Backpack
print("Tool Added")
end
end
end
end)```
I changed it to this
HOLY SHIT
IT WORKS
YEAHHHHHHHHHHH
bro not only does it work I understand it now
@tired cove in the future can I list something as a global value? how do I reference that
do I make a script in replicated storage with the global values?
wdym global value?
explain to me what a global value is that you meant
because my price is a Value
nvm it has to be local
okay I understand
thank you richard you were a big help
alr if you need anything from me i'm a ping away