Script help for a story game

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 hope I can get some help

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.

try this maybe

   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)
1 Like

Ohhh, Could then show a better exapmle so I can acces the dataStore?

could you show the client code that fires the event and the server code that listens the event so i can help?

Well ,the scrip is a localscript that fires the event “GetWinEvent”, but how do you mean?

also pls explain what are you making so i can understand

I am making this for a story game, so I can fire a event that will give the players a win.

This is what I use as script to fire the other scripts.

GetWinEvent is red underlined. Check if it is defined correctly.

Yeah, I have problems with that, so I need someone who can help me with that

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)

Does this work for you? Try it.

wait where do I need to put this?

Make a script in ServerScriptService.

Okay, then I need to?

Qucik note, there is already a stats

  • ikepike3333

You copy the code and paste that into the script.

Optional

You can name the script “PlrHandler” or any name you want

the wins section in underline red, oof

Oops, replace the “stage” with “wins”.

Okay, I did it, but the GetWinevent is the underlined

What? Send me a screenshot. I’ll try to see.

Here it is its still red