What do you want to achieve? Keep it simple and clear!
give tool after buying
What is the issue? Include screenshots / videos if possible!
tool is not giving after buying
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
no
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- local PlayerDataManager = require(game:GetService("ServerScriptService").PlayerDataManager)
local parent = script.Parent
local ClickDetector = parent.ClickDetector
local Price = script.Price
local ServerStorage = game:GetService("ServerStorage")
local tool = script.Value
local toolcloned = tool:Clone()
local Price = script.Price
ClickDetector.MouseClick:Connect(function(player, tool)
PlayerDataManager:GetPlayerDataReplica(player):andThen(function(Replica)
if Replica.Data.GnidaCoins >= 300 then
Replica:SetValue("GnidaCoins",Replica.Data.GnidaCoins-300)
toolcloned.Parent = player:WaitForChild("Backpack")
end
end)
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
--//Services
local Replicated = game:GetService("ReplicatedStorage")
local Modules = Replicated:WaitForChild('Modules')
local Players = game:GetService("Players")
local ReplicaService = require(Replicated.Replica.ReplicaServiceListeners)
local ProfileService = require(Modules.ProfileService)
local Promise = require(Modules.promise)
local SaveStructure = require(script.SaveStructure)
--//Variables
local Profiles = {}
local Replicas = {}
local PlayerProfileClassToken = ReplicaService.NewClassToken("PlayerData")
local ProfileStore = ProfileService.GetProfileStore("PlayerData", SaveStructure)
local PlayerDataManager = {}
--//Functions
function PlayerDataManager:GetPlayerDataReplica(Player: Player)
return Promise.new(function(Resolve, Reject)
assert(typeof(Player) == "Instance" and Player:IsDescendantOf(Players), "Value passed is not a valid player")
if not Profiles[Player] and not Replicas[Player] then
repeat
if Player and Player:IsDescendantOf(Players) then
wait()
else
Reject("Player left the game")
end
until Profiles[Player] and Replicas[Player]
end
local Profile = Profiles[Player]
local Replica = Replicas[Player]
if Profile and Profile:IsActive() then
if Replica and Replica:IsActive() then
Resolve(Replica)
else
Reject("Replica did not exist or wasn't active")
end
else
Reject("Profile did not exist or wasn't active")
end
end)
end
function PlayerDataManager:GetPlayerProfile(Player: Player)
return Promise.new(function(Resolve, Reject)
assert(typeof(Player) == "Instance" and Player:IsDescendantOf(Players), "Value passed is not a valid player")
if not Profiles[Player] and not Replicas[Player] then
repeat
if Player and Player:IsDescendantOf(Players) then
wait()
else
Reject("Player left the game")
end
until Profiles[Player] and Replicas[Player]
end
local Profile = Profiles[Player]
local Replica = Replicas[Player]
if Profile and Profile:IsActive() then
if Replica and Replica:IsActive() then
Resolve(Profile)
else
Reject("Replica did not exist or wasn't active")
end
else
Reject("Profile did not exist or wasn't active")
end
end)
end
local function PlayerAdded(Player: Player)
local StartTime = tick()
local Profile = ProfileStore:LoadProfileAsync("Player_" .. Player.UserId)
if Profile then
Profile:AddUserId(Player.UserId)
Profile:Reconcile()
Profile:ListenToRelease(function()
Profiles[Player] = nil
Replicas[Player]:Destroy()
Replicas[Player]= nil
Player:Kick("Profile was released")
end)
if Player:IsDescendantOf(Players) == true then
Profiles[Player] = Profile
local Replica = ReplicaService.NewReplica({
ClassToken = PlayerProfileClassToken,
Tags = {["Player"] = Player},
Data = Profile.Data,
Replication = "All"
})
Replicas[Player] = Replica
warn(Player.Name.. "'s profile has been loaded. ".."("..string.sub(tostring(tick()-StartTime),1,5)..")")
else
Profile:Release()
end
else
Player:Kick("Profile == nil")
end
end
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(PlayerAdded, player)
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(function(Player)
if Profiles[Player] then
Profiles[Player]:Release()
end
end)
return PlayerDataManager
I’m guessing you have the PlayerDataManager from elsewhere and using it to store and fetch the player’s data, meaning that ms and the ms’s it’s requiring are all fine? One issue is definitely that the first line is commented, which means you can’t use it, so you need to uncomment it.
I’d recommend you add some prints to verify that the tool exists, the clickdetector is actually clicked and that the transaction works.
Something similar to:
local PlayerDataManager = require(game:GetService("ServerScriptService").PlayerDataManager)
local parent = script.Parent
local ClickDetector = parent.ClickDetector
local Price = script.Price
local ServerStorage = game:GetService("ServerStorage")
local tool = script.Value
local toolcloned = tool:Clone()
local Price = script.Price
print(toolcloned.Name)
ClickDetector.MouseClick:Connect(function(player, tool)
print("CD was clicked!")
PlayerDataManager:GetPlayerDataReplica(player):andThen(function(Replica)
print("You have "..Replica.Data.GnidaCoins.." coins!")
if Replica.Data.GnidaCoins >= 300 then
Replica:SetValue("GnidaCoins",Replica.Data.GnidaCoins-300)
toolcloned.Parent = player:WaitForChild("Backpack")
print("You bought the tool, and it has been placed in "..toolcloned.Parent.Name)
end
end)
end)
-- local PlayerDataManager = require(game:GetService("ServerScriptService").PlayerDataManager)
local parent = script.Parent
local ClickDetector = parent.ClickDetector
local Price = script.Price
local ServerStorage = game:GetService("ServerStorage")
local tool = script.Value
local Price = script.Price
ClickDetector.MouseClick:Connect(function(player)
PlayerDataManager:GetPlayerDataReplica(player):andThen(function(Replica)
if Replica.Data.GnidaCoins >= 300 then
local newTool = tool:Clone()
Replica:SetValue("GnidaCoins",Replica.Data.GnidaCoins-300)
newTool.Parent = player.Backpack
end
end)
end)
Seems you have an error from the Replica module, probably coming from a WaitForChild that’s waiting indefinitely for a coin gui. Do you have a ScreenGUI called GnidaCoinsGui in StarterGui?
That’s not the issue, check the previously provided Screenshot of the output one more time and you will see that the currency has been taken away from the Player, meaning that the tool:Clone() shouldn’t be impacted by the infinite yield from that Waiting for a ScreenGui
I see what you mean and that could be true, however I believe that requiring is not giving the error, but rather running any function that (I’m blindly guessing) is trying to edit a textlabel showing how many coins the player has. Hence that printing the amount of coins works fine, because requiring the module doesn’t throw an error (or the error would show up much earlier) and the issue lies in
where I’m again blindly guessing that this function tries to tell the client to update it’s gui to show the new amount, but the client doesn’t have a gui so it tells it that would be impossible and then the server throws the error on the line right before giving the tool.
@pinconessmellweird can you try switching positions in the script of the two lines
So the script will try to give the tool before it takes away the money, as I believe this would avoid the error TEMPORARILY to see where the issue comes from
I understand that but look at it from a different perspective, who the hell would use :WaitForChild() on a ServerScript, and we know it’s a ServerScript because a LocalScript shouldn’t and hopefully wouldn’t have the ability to access / change the Data (currency)
True, I guess wanting a response from a remotefunction for changing a gui value doesn’t give sense.
Seems the issue is something else that comes from SetValue, perhaps the DataController. I find it really weird that it didn’t error on the first click, but the second click 5 seconds after suddenly error…
i delete objectvalue, things that associated with global leaderboards and after this script is working
– local ServerStorage = game:GetService(“ServerStorage”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local PlayerDataManager = require(game:GetService(“ServerScriptService”).PlayerDataManager)
local parent = script.Parent
local ClickDetector = parent.ClickDetector
local Price = script.Price
local TName = script.TName
local Tool = script.TName.Value
ClickDetector.MouseClick:Connect(function(player)
PlayerDataManager:GetPlayerDataReplica(player):andThen(function(Replica)
if Replica.Data.Cash >= 1 then
Replica:SetValue("Cash",Replica.Data.Cash+100)
Tool:Clone().Parent = player.Backpack
end
end)
end)