I know I made a post about this but this is a new issue
I made a remote event that tells the client to update the gui to the coins value whenever a zombie dies in the output it says
FireClient: player argument must be a Player object
1- I don’t know how to increase the Coins value to 5 after a zombie dies
2-I don’t know how to update the gui to the coins value
Here is my code:
Server
local Coins = game.ReplicatedStorage.Coins
local player = game.Players
script.Parent.Died:Connect(function()
game.ReplicatedStorage.ZombieAmount.Value -= 1
Coins:FireClient(player)
end)
I have a data store script in server script service
-- Setting data store
local DataStore = game:GetService("DataStoreService")
local PlayersData = DataStore:GetDataStore("PlayersData")
-- Making the coin when the player joins
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Streak = Instance.new("IntValue")
Streak.Name = "Streak"
Streak.Parent = leaderstats
local coin = Instance.new("IntValue")
coin.Name = "Coin"
coin.Parent = leaderstats
end)
-- Saving the coin
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
PlayersData:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
end)
if success then
print("Player's data successfully saved")
else
print("Bruh")
warn(errormessage)
end
end)
In your server script, instead of referencing a player, you just reference the Players Service. You have to point the variable to a specific player instance.
I assume, your Script is parented to a character’s humanoid.
In this case, you can get the player from the character.
script.Parent.Died:Connect(function()
local character = script.Parent.Parent
local player = game.Players:GetPlayerFromCharacter(character)
game.ReplicatedStorage.ZombieAmount.Value -= 1
Coins:FireClient(player)
end)
stop accessing player from server script, you cant do that instead you should fire remote event from client to server, server gets player variable (automatically added, you dont have to add that during FireServer()) and then make the server change the cash.
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Streak = Instance.new("IntValue" , leaderstats) --Sets up value for leaderstats
Streak.Name = "Streak"
local Coin = Instance.new("IntValue" , leaderstats) --Sets up value for leaderstats
Coin.Name = "Coin"
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
Streak.Value = data['Streak']
Coin.Value = data['Coin']
else
-- Data store is working, but no current data for this player
Streak.Value = 0
Coin.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats)
end)
if success then
warn('Successfully saved player data!')
elseif not success then
warn('There was an error saving the players data: ' .. err)
end
end
Players.PlayerAdded:Connect(onPlayerJoin)
Players.PlayerRemoving:Connect(onPlayerExit)
Well because if a player purchased something in game that costs for example 400 coins
all players will lose 400 coins so i thought to fix that i must make it a local script
local remote
local itemPricingList = {
["example"] = 100;
}
remote.OnServerEvent:Connect(function(player, item: string)
if not itemPricingList[item] then return end
if player.leaderstats.Coins.Value > itemPricingList[item] then
-- do stuff
end
end)
client:
local remote
local function purchase(item: string)
remote:FireServer(item)
end
-- example usage
purchase("example")