Heya devs, I have some plroblems with a script, I want to use a script that counts a win to the Datastore, but it seems to be not working.
Here is the script:
local dataStore = game:GetService("DataStoreService"):GetOrderedDataStore("Wins")
local GetWinEvent = game.ReplicatedStorage.GetWinEvent
GetWinEvent.OnClientEvent:Connect(function(dataStore,Wins)
local player = game.Players:GetPlayerFromCharacter(character)
local character = game.Parent
dataStore:IncrementAsync(player.UserId, 1)
end)
I assume that this is a LocalScript (you used .OnClientEvent.) Keeping this in mind, you can’t make changes to a DataStore in a LocalScript, in fact you can’t even access the service if I recall right.
local dataStore = game:GetService("DataStoreService"):GetOrderedDataStore("Wins")
local GetWinEvent = game.ReplicatedStorage.GetWinEvent
GetWinEvent.OnServerEvent:Connect(function(dataStore,Wins)
local player = game.Players:GetPlayerFromCharacter(character)
local character = game.Parent
dataStore:IncrementAsync(player.UserId, 1)
end)
local dds = game:GetService("DataStoreService")
local dataStore = dds:GetDataStore("Values")
local GetWinEvent = game.ReplicatedStorage.GetWinEvent
--I'll just add this if you want it.
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = plr
local stage = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = stats
local data = dataStore:GetAsync(plr.UserId)
if data then
print(data.Wins)
for _,stat in pairs(stats:GetChildren()) do
stat.Value = data[stat.Name]
end
else
print(plr.Name .. " has no data")
end
end)
GetWinEvent.OnClientEvent:Connect(function()
local player = game.Players:GetPlayers()
local character = player.Character
dataStore:IncrementAsync(player.UserId, 1)
end)