Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Datastore script for saving pet data of a player doesn't work?

Hi, so I have a buy button with a local script inside which is as follows:

local cost = 2500
local petName = "Tiny Noob"
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	print("Sending transaction request to server...")
	game.ReplicatedStorage.purchasePet:FireServer(player, cost, petName)
	print("petEvent was fired!")
end)

It will make two prints, one to indicate the button has been clicked and one to indicate that the remoteevent was fired. This fires to a Module script in ServerScriptService.

local ItemSaveManager = {}

local DataStore = game:GetService("DataStoreService")
local playerData = DataStore:GetDataStore("ItemSaveSystem")

local Players = game:GetService("Players")

local AUTOSAVE_INTERVAL = 60

local DataTable = {}

function ItemSaveManager:UpdateValue(player, subTable, value)
    local playerUserId = "Player_" .. player.UserId	

    table.insert(DataTable[playerUserId][subTable], value)

end

function ItemSaveManager:CheckInTable(player, subTable, value)
    local playerUserId = "Player_" .. player.UserId
    local sendBack = false
    for _, v in pairs(DataTable[playerUserId][subTable]) do
        if v == value then
	        sendBack = true
        end
    end
    return sendBack
end

Players.PlayerAdded:connect(function(player)
    local playerUserId = "Player_" .. player.UserId

    local success, data = pcall(function()
        return playerData:GetAsync(playerUserId)
    end)
    if success then
        if data then
            DataTable[playerUserId] = data
        
        else
            DataTable[playerUserId] = {}

            DataTable[playerUserId]["Pets"] = {}

            playerData:SetAsync(playerUserId, DataTable)        
        end
    else
        warn("Cannot acces data store for player:" .. player.UserId)
    end
end)

game.Players.PlayerAdded:Connect(function(player)
    if ItemSaveManager:CheckInTable(player, "Pets", "Tiny Noob") then
        print(player.. " has pet")
		game.ReplicatedStorage.givePet:FireClient(player)
    end
end)

local function onPurchasePetEvent(player, cost, petName)
	print("Script has found player!")
	if player.leaderstats.Quacks.Value >= tonumber(cost) then
        player.leaderstats.Quacks.Value = player.leaderstats.Cash.Value - tonumber(cost)
        ItemSaveManager:UpdateValue(player, "Pets" , petName)
        print("Transaction completed for Player")
		game.ReplicatedStorage.givePet:FireClient(player)
    end
end

game.ReplicatedStorage.purchasePet.OnServerEvent:Connect(onPurchasePetEvent)

local function savePlayerData(playerUserId)
    if DataTable[playerUserId] then
	    local success, err = pcall(function()
	        playerData:SetAsync(playerUserId, DataTable[playerUserId])
	    end)
	    if not success then
		    warn("Cannot save data for player!")
	    end
    end
end

local function saveOnExit(player)
    local playerUserId = "Player_" .. player.UserId
    savePlayerData(playerUserId)
    print(player.name .. "s data saved")
end

local function autoSave()
    while wait(AUTOSAVE_INTERVAL) do
	    for playerUserId, data in pairs(DataTable) do
		    savePlayerData(playerUserId)
	    end
	    print("Auto Saved")
    end
end

spawn(autoSave)

game.Players.PlayerRemoving:Connect(saveOnExit)

return ItemSaveManager

Let's ignore most of the script and focus on this part:

local function onPurchasePetEvent(player, cost, petName)
	print("Server received event")
	if player.leaderstats.Quacks.Value >= tonumber(cost) then
        player.leaderstats.Quacks.Value = player.leaderstats.Cash.Value - tonumber(cost)
        ItemSaveManager:UpdateValue(player, "Pets" , petName)
        print("Transaction completed for Player")
		game.ReplicatedStorage.givePet:FireClient(player)
    end
end

game.ReplicatedStorage.purchasePet.OnServerEvent:Connect(onPurchasePetEvent)

The problem is that I'm not getting any prints for "Server received event". This module script just saves a model of the pets and the pet data of players so when a player joins and owns a pet they will spawn with one. I'm not getting any error in output and no prints are received. What's wrong with this script?

1 answer

You should never send the cost value to the server, the exploiter can simple edit that in order to get cheaper items or whatever you're selling. Check all of this on the server.

Are you sure the FireServer() request is being sent in the first place?

2 comments

0

It shows the request was fired otherwise it wouldn't print, though there are no prints for it being received.

0

Are you having the OnServerEvent and FireClient in the same script? Then that's why. FireClient is used by a localscript, and OnServerEvent is used by the server to receive the request the client sent.

This discussion is preserved as it was. New answers, comments, and votes are closed.