Code Chat Command

Great! In that case, you can skip the line where we’re initializing the CoastBux value and instead call IncrementAsync directly on the DataStore for leaderstats.CoastBux. Here’s the updated redeemCode function:

local DataStoreService = game:GetService("DataStoreService")
local CODES_REDEEMED_DATASTORE_KEY = "CodesRedeemed"
local CODES_DATASTORE_KEY = "Your_Datastore_Key_Here"

function validateCode(code)
    local codesData = DataStoreService:GetDataStore(CODES_DATASTORE_KEY)
    local success, reward = pcall(codesData.GetAsync, codesData, code)
    if success and reward then
        return reward
    end
end

function redeemCode(player, args)
    local code = args[1]
    if not code then
        player:Kick("Invalid command usage. Usage: !redeem code")
        return
    end
    local reward = validateCode(code)
    if not reward then
        player:Kick("Invalid code")
        return
    end
    local codesRedeemedDataStore = DataStoreService:GetDataStore(CODES_REDEEMED_DATASTORE_KEY)
    local success, codesRedeemed = pcall(codesRedeemedDataStore.GetAsync, codesRedeemedDataStore, "userid_" .. tostring(player.UserId))
    if success then
        if codesRedeemed and codesRedeemed[code] then
            player:Kick("You have already redeemed this code")
            return
        end
    else
        player:Kick("Failed to retrieve redeemed codes. Please try again later.")
        return
    end
    local playerDataStore = DataStoreService:GetDataStore(COASTBUX_DATASTORE_KEY)
    local success, currentCoastBux = pcall(playerDataStore.IncrementAsync, playerDataStore, "userid_" .. tostring(player.UserId), reward)
    if success then
        player.leaderstats.CoastBux.Value = currentCoastBux
        player:SendNotification(reward .. " added to your CoastBux. Your current balance is: " .. tostring(currentCoastBux))
        local codeRedeemedData = codesRedeemed or {}
        codeRedeemedData[code] = true
        codesRedeemedDataStore:SetAsync("userid_" .. tostring(player.UserId), codeRedeemedData)
    else
        player:Kick("Failed to add CoastBux. Please try again later.")
    end
end

In this updated version, we’re using the existing COASTBUX_DATASTORE_KEY to access the DataStore for the player’s CoastBux balance directly, and then calling IncrementAsync on that datastore to add the reward directly to the player’s CoastBux balance.

Let me know if you have any other questions or concerns!

Could you make a script with everything we’ve done?

Certainly! Here’s a full script that includes everything we’ve done so far:

local DataStoreService = game:GetService("DataStoreService")

local CODES_DATASTORE_KEY = "Your_Datastore_Key_Here"
local CODES_REDEEMED_DATASTORE_KEY = "CodesRedeemed"
local COASTBUX_DATASTORE_KEY = "CoastBux"

function validateCode(code)
    local codesData = DataStoreService:GetDataStore(CODES_DATASTORE_KEY)
    local success, reward = pcall(codesData.GetAsync, codesData, code)
    if success and reward then
        return reward
    end
end

function redeemCode(player, args)
    local code = args[1]
    if not code then
        player:Kick("Invalid command usage. Usage: !redeem code")
        return
    end
    local reward = validateCode(code)
    if not reward then
        player:Kick("Invalid code")
        return
    end
    local codesRedeemedDataStore = DataStoreService:GetDataStore(CODES_REDEEMED_DATASTORE_KEY)
    local success, codesRedeemed = pcall(codesRedeemedDataStore.GetAsync, codesRedeemedDataStore, "userid_" .. tostring(player.UserId))
    if success then
        if codesRedeemed and codesRedeemed[code] then
            player:Kick("You have already redeemed this code")
            return
        end
    else
        player:Kick("Failed to retrieve redeemed codes. Please try again later.")
        return
    end
    local playerDataStore = DataStoreService:GetDataStore(COASTBUX_DATASTORE_KEY)
    local success, currentCoastBux = pcall(playerDataStore.IncrementAsync, playerDataStore, "userid_" .. tostring(player.UserId), reward)
    if success then
        player.leaderstats.CoastBux.Value = currentCoastBux
        player:SendNotification(reward .. " added to your CoastBux. Your current balance is: " .. tostring(currentCoastBux))
        local codeRedeemedData = codesRedeemed or {}
        codeRedeemedData[code] = true
        codesRedeemedDataStore:SetAsync("userid_" .. tostring(player.UserId), codeRedeemedData)
    else
        player:Kick("Failed to add CoastBux. Please try again later.")
    end
end

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local leaderstats = Instance.new("Folder")
        leaderstats.Name = "leaderstats"
        leaderstats.Parent = player
        
        local coastBux = Instance.new("IntValue")
        coastBux.Name = "CoastBux"
        coastBux.Value = 0
        coastBux.Parent = leaderstats
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local codesRedeemedDataStore = DataStoreService:GetDataStore(CODES_REDEEMED_DATASTORE_KEY)
    codesRedeemedDataStore:SetAsync("userid_" .. tostring(player.UserId), nil)
end)

game:GetService("ReplicatedStorage").RedeemCode.OnServerInvoke = redeemCode

This script includes everything we’ve covered so far:

  • The validateCode function to check if a provided code is valid
  • The redeemCode function to redeem valid codes and add the reward to a player’s CoastBux balance
  • Setting up leaderstats and CoastBux for each player when they join
  • Removing the stored redeemed codes for a player when they leave
  • Connecting redeemCode as the function to be called when the RedeemCode remote event is invoked on the server

Make sure to replace the placeholder value for CODES_DATASTORE_KEY with the actual key for your DataStore that stores valid codes. Let me know if you have any other questions or concerns!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.