Hey guys, can someone help me with my server script? The remote is getting fired from the local to the server, so it will change the int value number information, but that applies to all players in the game. How can I make it so it only updates the value for the player?
Here is my server script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectRemote = ReplicatedStorage.remoteEvents.Collect
local SwimBoostRemote = ReplicatedStorage.remoteEvents.SwimBoost
CollectRemote.OnServerEvent:Connect(function(Player)
local PlayerCoins = Player:WaitForChild("leaderstats"):WaitForChild("Coins")
local CSPPrice = ReplicatedStorage.Upgrades.CSP.Price.Value
local CSPLevel = ReplicatedStorage.Upgrades.CSP.Level.Value
if CSPLevel < 12 and PlayerCoins.Value >= CSPPrice then
PlayerCoins.Value = PlayerCoins.Value - CSPPrice
CSPPrice = CSPPrice * 2
CSPLevel = CSPLevel + 1
ReplicatedStorage.Upgrades.CSP.Level.Value = CSPLevel
ReplicatedStorage.Upgrades.CSP.Price.Value = CSPPrice
end
end)
SwimBoostRemote.OnServerEvent:Connect(function(Player)
local PlayerCoins = Player:WaitForChild("leaderstats"):WaitForChild("Coins")
local SBPrice = ReplicatedStorage.Upgrades.SB.Price.Value
local SBLevel = ReplicatedStorage.Upgrades.SB.Level.Value
if SBLevel < 1 and PlayerCoins.Value >= SBPrice then
PlayerCoins.Value = PlayerCoins.Value - SBPrice
SBPrice = SBPrice * 2
SBLevel = SBLevel + 1
ReplicatedStorage.Upgrades.SB.Level.Value = SBLevel
ReplicatedStorage.Upgrades.SB.Price.Value = SBPrice
end
end)
The RemoteEvents aren’t the problem, you’re using them properly.
However, you’re changing the values to CSP and SB for all players because all players share the same Upgrade values. So if player A spends the upgrade, only player A will lose coins but everyone in the server will get the benefits.
To achieve an upgrade system where only player A gets the benefits and not everyone in the server, one way to achieve this is to store the Upgrade values in the Player whenever they join, and then updating the upgrade price and level from there, which can be done using PlayerAdded:
-- This function is tied to an event. The function will fire whenever a player joins the server.
game.Players.PlayerAdded:Connect(function(NewPlayer)
-- Copy the Upgrades object into the new player
local PlayerUpgrades = ReplicatedStorage.Upgrades:Clone()
PlayerUpgrades.Parent = NewPlayer
end)
CollectRemote.OnServerEvent:Connect(function(Player)
-- Error handling
if Player:FindFirstChild("Upgrades") == nil then
error("Attempted to perform an upgrade but <" ..Player.Name.. "> Upgrades object does not exist!")
end
-- Get the amount of coins the player has
local PlayerCoins = Player:WaitForChild("leaderstats"):WaitForChild("Coins")
-- Get the cost of the CSP upgrade and the level of the player's CSP specifically
local CSPPrice = Player.Upgrades.CSP.Price.Value
local CSPLevel = Player.Upgrades.CSP.Level.Value
-- Conditional if CSP's level is less than 12, and player can afford the upgrade cost
if CSPLevel < 12 and PlayerCoins.Value >= CSPPrice then
-- Spend player's coins to double the next level upgrade's cost and increase the CSP level for the player by 1.
PlayerCoins.Value = PlayerCoins.Value - CSPPrice
CSPPrice = CSPPrice * 2
CSPLevel = CSPLevel + 1
-- Update the cost and current level of CSP for the player
Player.Upgrades.CSP.Level.Value = CSPLevel
Player.Upgrades.CSP.Price.Value = CSPPrice
end
end)
SwimBoostRemote.OnServerEvent:Connect(function(Player)
...
Now, the Upgrades object in ReplicatedStorage acts as a “template” of upgrades that is inserted into a player’s instance whenever they join the server. When inside the player, the values will be unique to them, therefore, upgrading will only affect the player that chooses the upgrade.
Basically when you have variables that are being added / subtracted by itself + or - a certain amount, instead of writing variable = variable + amount you can write it as variable += amount.