I have a script in server script service which saves some values that are add to the player when they join. I'm saving those values to save purchases of a player. Btw FE is enabled

server script:


local dss = game:GetService("DataStoreService")
local ds1 = dss:GetDataStore("carSave")

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteSave = replicatedStorage:WaitForChild("RemoteSave")


game.Players.PlayerAdded:Connect(function(player)
	local ownCar1 = Instance.new("IntValue", player)
	ownCar1.Name = "Own1"
	local ownCar2 = Instance.new("IntValue", player)
	ownCar2.Name = "Own2"
	local ownCar3 = Instance.new("IntValue", player)
	ownCar3.Name = "Own3"
	local ownCar4 = Instance.new("IntValue", player)
	ownCar4.Name = "Own4"
	
	--getting data
	ownCar1.Value = ds1:GetAsync(player.UserId, ownCar1.Value) or 0
	ownCar2.Value = ds1:GetAsync(player.UserId, ownCar2.Value) or 0
	ownCar3.Value = ds1:GetAsync(player.UserId, ownCar3.Value) or 0
	ownCar4.Value = ds1:GetAsync(player.UserId, ownCar4.Value) or 0
	
	print("got data for "..player.Name)
	
	remoteSave:FireClient(player, ownCar1.Value, ownCar2.Value, ownCar3.Value, ownCar4.Value)	
	
end)


remoteSave.OnServerEvent:Connect(function(plr, own)
	if own == "car1" then
		plr.Own1.Value = 1
		ds1:SetAsync(plr.UserId, plr.Own1.Value)
		print(plr.Name.." saved data for car1")
	elseif own == "car2" then
		plr.Own2.Value = 1
		ds1:SetAsync(plr.UserId, plr.Own2.Value)
		print(plr.Name.." saved data for car2")
	elseif own == "car3" then
		plr.Own3.Value = 1
		ds1:SetAsync(plr.UserId, plr.Own3.Value)
		print(plr.Name.." saved data for car3")
	elseif own == "car4" then
		plr.Own4.Value = 1
		ds1:SetAsync(plr.UserId, plr.Own4.Value)
		print(plr.Name.." saved data for car4")
	end
end)


game.Players.PlayerRemoving:Connect(function(pl)
	print(pl.Own1.Value)
	print(pl.Own2.Value)
	print(pl.Own3.Value)
	print(pl.Own4.Value)
	ds1:SetAsync(pl.UserId, pl.Own1.Value)
	print(pl.Name.." saved data with "..pl.Own1.Value.." car1")
	ds1:SetAsync(pl.UserId, pl.Own2.Value)
	print(pl.Name.." saved data with "..pl.Own2.Value.." car2")
	ds1:SetAsync(pl.UserId, pl.Own3.Value)
	print(pl.Name.." saved data with "..pl.Own3.Value.." car3")
	ds1:SetAsync(pl.UserId, pl.Own4.Value)
	print(pl.Name.." saved data with "..pl.Own4.Value.." car4")
end)

and I also have a local script that is connected with this script using remote events. This local script basically allows players to purchase a car and when they do I want to fire server to change those values I have and save them

local script:


local player = game.Players.LocalPlayer
local replicatedStorage = game:GetService("ReplicatedStorage")

local remote1 = replicatedStorage:WaitForChild("Remote1")
local remote2 = replicatedStorage:WaitForChild("Remote2")
local remote3 = replicatedStorage:WaitForChild("Remote3")
local remoteSave = replicatedStorage:WaitForChild("RemoteSave")

local screenGui = script.Parent
local frame = screenGui:WaitForChild("Frame")
local carsFrame = screenGui:WaitForChild("carsFrame")
local purchase = frame:WaitForChild("PurchaseButton")
local cancel = frame:WaitForChild("CancelButton")

local car1Button = carsFrame:WaitForChild("car1Button")
local car2Button = carsFrame:WaitForChild("car2Button")
local car3Button = carsFrame:WaitForChild("car3Button")
local car4Button = carsFrame:WaitForChild("car4Button")


--purchasing cars
purchase.MouseButton1Click:Connect(function()
	if carToPurchase == "car1" then
		print("car1 successfully purchased")
		player.Character.Humanoid.WalkSpeed = 16
		frame.Visible = false
		purchaseSound:Play()
		car1Button.Visible = true
		remoteSave:FireServer("car1") --here it fires the server
	elseif carToPurchase == "car2" then
		print("car2 successfully purchased")
		player.Character.Humanoid.WalkSpeed = 16
		frame.Visible = false
		purchaseSound:Play()
		car2Button.Visible = true
		remoteSave:FireServer("car2")
	elseif carToPurchase == "car3" then
		print("car3 successfully purchased")
		player.Character.Humanoid.WalkSpeed = 16
		frame.Visible = false
		purchaseSound:Play()
		car3Button.Visible = true
		remoteSave:FireServer("car3")
	elseif carToPurchase == "car4" then
		print("car4 successfully purchased")
		player.Character.Humanoid.WalkSpeed = 16
		frame.Visible = false
		purchaseSound:Play()
		car4Button.Visible = true
		remoteSave:FireServer("car4")
	end
end)

remoteSave.OnClientEvent:Connect(function(own1, own2, own3, own4)
	print("getting data from server")
	
	if own1 == 1 then
		print(player.Name.." owns car1")
		car1Button.Visible = true
	end	
	if own2 == 1 then
		print(player.Name.." owns car2")
		car2Button.Visible = true
	end
	if own3 == 1 then
		print(player.Name.." owns car3")
		car3Button.Visible = true
	end
	if own4 == 1 then
		print(player.Name.." owns car3")
		car4Button.Visible = true
	end
end)

the variable carToPurchase isn't really important for this because it works good so i removed the part of a script where it's defined.

Anyways when i purchase "car1" it prints that i saved car1 but then when i join again it makes all 4 buttons visible so it appears it saved all 4 values as 1.

Any ideas how to fix it?

1 comment

0

Well on lines 19-22 you are using GetAsync wrong. It only takes one parameter and that's the key you're searching for in your data store.