>= Value Not Checking For Leaderstats

I am currently working on a shop Gui using Click Detectors in the world. However, that is not where the issue lies. I have a >= operator that checks if an item in the players leaderstats is >= to 100 but it doesn’t seem to be checking. Curiously, if I change it to a == than it checks for that.

local click = script.Parent
local button = click.Parent
local hoverSound = script.hover
local clickSound = script.click
local leaveSound = script.leave

click.MouseHoverEnter:Connect(function(plr)
	button.CFrame = CFrame.new(button.CFrame.X, button.CFrame.Y, 148.502)
	hoverSound:Play()
	script.Parent.MouseClick:Connect(function(plr)
		clickSound:Play()
		if plr.leaderstats.Coconuts.Value >= 100 then
			plr.leaderstats.Coconuts.Value -= 100
		end
	end)
end)

click.MouseHoverLeave:Connect(function(plr)
	button.CFrame = CFrame.new(button.CFrame.X, button.CFrame.Y, 148.351)
	leaveSound:Play()
end)

I am receiving errors, but I think they are unrelated.

just give us the error, and try to use print or pcall to check where it gone wrong, and give us the image if the explorer of the script.Parent

17:22:59.108 ServerScriptService.Datastore:18: attempt to call a number value - Server - Datastore:18 and 17:22:52.651 Unable to cast value to Object - Server - Datastore:13

it’s the error on the datastore, have you connect the datastore to the coconut value?

I am new to datastores and have followed a tutorial to achieve it. Here is the code for the datastore

local dataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrid = "id_"..plr.UserId
	local save1 = plr.leaderstats.Coconuts
	
	local GetSaved = dataStore:GetAsync(plrid)
	if GetSaved then
		save1.Value = GetSaved[1]
	else
		local NumberForSaving = {save1.Value}
		dataStore:GetAsync(plrid,NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	dataStore:SetAsync("id_"..plr.UserId{plr.leaderstats.Coconuts.Value})
end)

you can try this module, it help the datastore

1 Like

first you assing it into replicatestorage then you create another module, name it PlayerDataHandler, and copy this script

--Declare
local Module = game.ReplicatedStorage.DataStore.Module -- change this script to the directory that you put then profile service module at

--Main
local PlayerDataHandler = {}

local DataTemplate = {
	DCoin = 0,
	Cash = 10
}

local ProfileService = require(Module.ProfileService)
local Players = game.Players

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerProfile",
	DataTemplate
)

local Profiles = {}

local function playerAdded(player)
	local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
	
	if profile then
		profile:AddUserId(player.UserId)
		profile:Reconcile()
		
		profile:ListenToRelease(function()
			Profiles[player] = nil
			
			player:Kick()
		end)
		
		if not player:IsDescendantOf(Players) then
			profile:Release()
		else
			Profiles[player] = profile
			
			print(Profiles[player].Data)
		end
	else
		player:Kick()
	end
end

function PlayerDataHandler:Init()
	for _, player in game.Players:GetPlayers() do
		task.spawn(playerAdded, player)
	end
	
	game.Players.PlayerAdded:Connect(playerAdded)
	
	game.Players.PlayerRemoving:Connect(function(player)
		if Profiles[player] then
			Profiles[player]:Release()
		end
	end)
end

local function GetProfile(player)
	assert(Profiles[player], string.format("Profile does not exist for %s", player.UserId))

	return Profiles[player]
end

--Getter/Setter method
function PlayerDataHandler:Get(player, key)
	local profile = GetProfile(player)
	assert(profile.Data[key], string.format("Data does not exist for key %s", key))
	
	return profile.Data[key]
end

function PlayerDataHandler:Set(player, key, Value)
	local profile = GetProfile(player)
	assert(profile.Data[key], string.format("Data does not exist for key %s", key))
	
	assert(type(profile.Data[key]) == type(Value))
	
	profile.Data[key] = Value
end

function PlayerDataHandler:Update(player, key, callback)
	local profile = GetProfile(player)
	
	local oldData = self:Get(player, key)
	local newData = callback(oldData)
	
	player.leaderstats:FindFirstChild(key).Value = newData
	
	self:Set(player, key, newData)
end

return PlayerDataHandler
1 Like

then change the datatemplate to the data you want