#linking datastore to purchase event issue

171 messages · Page 1 of 1 (latest)

dry helm
#

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

tired cove
#

Put the purchase event in the same script as the datastore, it will allow you to manipulate the data without excessive coding

dusky shoreBOT
#

studio** You are now Level 1! **studio

dry helm
#

I just added it to the bottom of datastoreservice

tired cove
#

Also

#

Can you specify linking it to datastore

dry helm
#

actually I got confused im trying to link it to my already existing leaderstats value

#

the datastore is fine I was just confused

dry helm
# dry helm

is this okay though? I just added the purchasevent script to the bottom of datastore

tired cove
dry helm
#

okay but how do I get the shop script to subtract and actually give me the item

dusky shoreBOT
#

studio** You are now Level 2! **studio

dry helm
#

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)```
tired cove
#

Can you do some debugging and show me the errors

dry helm
#

okay hang on

tired cove
#

Datastore can't be accessed on client

dry helm
#
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

tired cove
#

The datastore might be what's breaking your script

dry helm
#

itd be datastore service line

#

line 8

#

but how do I fix it?

tired cove
#

They can't be accessed on the client to prevent exploits from deleting their data

dry helm
#

yeah that makes sense

tired cove
dry helm
#

this right?

tired cove
#

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

dry helm
#

I dont understand that

tired cove
#

Yeah datastore is quite complicated

dry helm
#

my datastore works fine but the issue is tryna subtract from it when I buy a weapon

#

from leaderstats

tired cove
#

I've been going in circles sorry

#

Just delete some datastore related stuff on the client script

#

It will prob work

dry helm
#

what do I replace it with

tired cove
#

Need me to explain it again in a more human way?

#

And not an AI?

dry helm
#

I deleted line 8

#

now it says this

#

still not giving me the weapon

dry helm
tired cove
dusky shoreBOT
#

studio** You are now Level 2! **studio

dry helm
#

theres no global value named currencyDataStore

#

idk what to set it as

#

if I cant use datastore service on client

tired cove
dry helm
#

what can I set it to if I cant use datastoreservice on client

tired cove
#

Delete that too

dry helm
#

the entire line?

tired cove
#

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

dry helm
tired cove
#

Cool

dry helm
#

now it says this

#

I gotta set a variable for currency I think

tired cove
#

There's no currency there

dry helm
#

and a variable for weapon price

tired cove
#

Yes

#

Weapon price is fine

#

But currency is nil

dry helm
#

oh yeah

#

cuz its local

tired cove
#

Have you learned about how to grab the player that the script runs for?

dry helm
tired cove
dry helm
#

this is it right

tired cove
#

Prob

dry helm
dusky shoreBOT
#

studio** You are now Level 3! **studio

dry helm
tired cove
#

Btw

#

You forgot .Value

dry helm
#

ohhhh on currency

tired cove
#

Yes

#

Alr good luck on your project, tell me if you need more help

dry helm
#

theres no error for it

#

but its still not subtracting from the value

tired cove
#

Add more debugging

#

Wait

#

Lemme go upstairs rq

dry helm
#

take your time

tired cove
#

send the server script again?

dry helm
#

the datastore script?

tired cove
dry helm
#
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```
dry helm
#

its too long

tired cove
#

i saw that

#

so

#

you can add a few print lines

#

before and after some events

#

to check which part is wrong

dry helm
#

in the purchase handler script right

tired cove
#

in the PurchaseEvent.OnServerFire

#

the datastore

dry helm
#

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?

tired cove
#

after crucial parts

dry helm
#

yeah it says in console that findinfo worked

#

so thats okay

tired cove
#

alr

dusky shoreBOT
#

studio** You are now Level 3! **studio

tired cove
#

how about the if price and tool

dry helm
#
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

tired cove
#

yes

dry helm
#

alr lemme run it

tired cove
#

coding is just trial and errors really

dry helm
#

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?

dusky shoreBOT
#

studio** You are now Level 4! **studio

dry helm
#
        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?

dry helm
#

do I gotta use localscript every time?

#

I mean local

#

local = yada yada

tired cove
#

explain to me what a global value is that you meant

dry helm
#

because my price is a Value

#

nvm it has to be local

#

okay I understand

#

thank you richard you were a big help

tired cove