[SOLVED] How do i access data in a localscript

I dont know how to transfer player data to a localscript, I tried using a remote event and returning the data but it just gives me an error: Attempt to index nil with cash, here is the onserverevent: getdata.OnServerEvent:Connect(function(plr)
return data[plr.UserId]
end)

2 Likes

What does data table contains and can u show the local script

1 Like

yeah, i can show you the localscript

1 Like

local plr = game:GetService(“Players”).LocalPlayer
local RS = game:GetService(“ReplicatedStorage”)
local remotes = RS:WaitForChild(“Remotes”)
local functions = RS:WaitForChild(“Functions”)
local getdata = remotes.GetData

function givecash(plr)
local data = getdata:FireServer(player)
data.Cash += 10
end

remotes.CashEvent.OnClientEvent:Connect(givecash)

1 Like

also the data table only contains cash as of right now.

1 Like

The parameter is plr but you are firing server to “player”. Typo? Also just give the cash in the server, exploiters will cheese the system if you do this in the client

1 Like

i will try changing the parameter

1 Like

Can u show me how the data table should be assuming this works cause i might have found the solution

1 Like

yes i can just let me try drmysteries solution

1 Like

It doesnt matter since remotr events pass player as first argument always

1 Like

He’s receiving it on the client, there is no player param. That plr param in the function is actually useless since it would be nil if he didn’t define the local player up there

1 Like

doesnt work, it just gives the same error

1 Like

He is firing from client side first

1 Like

also im sorry about this lol, im new to luau

1 Like

Can u show me what i ve said earlier

1 Like

Thought you were talking about the receiving thing. Anyway, you aren’t supposed to give a player param when you’re firing server, you will receive it on server side

1 Like

yeah, i will show the script right now

1 Like

local dss = game:GetService(“DataStoreService”)
local GlobalData = dss:GetDataStore(“GlobalData”)
local getdata = game:GetService(“ReplicatedStorage”).Remotes:WaitForChild(“GetData”)

local Playerdata
local data

game.Players.PlayerAdded:Connect(function(player)
local id = player.UserId

local success, result = pcall(function()
data = GlobalData:GetAsync(id)
end)

if success then
print(“Data loaded”)
else
print(“Error loading data”)
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local id = player.UserId
local data = {
[“Cash”] = 15
}

local success, result = pcall(function()
	GlobalData:SetAsync(id, data)
end)

if success then
	print("Saved")
else
	warn(result)
end

end)

getdata.OnServerEvent:Connect(function(plr)
return data[plr.UserId]
end)

1 Like

Euh… first change local data to local data = {}

1 Like

If the problem still occurs can u add print to the data of the plr
getdata.OnServerEvent:Connect(function(plr)
print(data[plr.UserId])
return data[plr.UserId]
end)

1 Like