Hi everyone, I have a problem. When a player touches a part, the value increases not only for the player but for all players on the server. I’m using ProfileService. How to fix this. I will be grateful for your help. Here are the scripts:
local StageCoinPlus = game.ReplicatedStorage:WaitForChild("StageCoinPlus")
local Players = game:GetService("Players")
local PlayerData = require(game.ServerScriptService.PlayerData:WaitForChild("Manager"))
StageCoinPlus.OnServerInvoke = function(player)
for _, player in Players:GetPlayers() do
local profile = PlayerData.Profiles[player]
if not profile then return end
profile.Data.StageCoinsValue += 100
player.StatsFolder.StageCoinsValue.Value += 100
end
end
This script changes the value, Using the Remote function. Script from ServerScriptService.
local Parted = game.Workspace:WaitForChild("Parted")
local StageCoinPlus = game.ReplicatedStorage:WaitForChild("StageCoinPlus")
local player = game.Players.LocalPlayer
Parted.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if Parted.CanTouch == true then
Parted.CanTouch = false
StageCoinPlus:InvokeServer(player)
end
end
wait(5)
Parted.CanTouch = true
end)
in this script, when the player touches the part, its value should change. But the value changes for all players. Script from StarterPlayerScript
I’ve had this issue before and I did not use ProfileService, here is a simpler method without it. Try this:
Script inside ServerScriptService:
local addAmount = 1 -- change to the amount added
game.ReplicatedStorage.StageCoinPlus.OnServerEvent:Connect(function(plr)
local leaderstat = game.Players:findFirstChild(plr.Name).leaderstats.LEADERSTATnameHere
LEADERSTATnameHere.Value = LEADERSTATnameHere.Value + addAmount
end)
Make sure you change the name of LEADERSTATnameHere to your leaderstat.
And here is the script that fires the event inside of the part:
local refreshTime = 5 -- how long for script cooldown
function onTouched(hit)
game.ReplicatedStorage.StageCoinPlus:FireAllClients()
script.Enabled = false -- so it does not spam this script
wait (refreshTime)
script.Enabled = true -- script can run again
end
script.Parent.Touched:connect(onTouched)
Note: This script was produced without the information of the leaderstats script where you have created the leaderstats for each player.
Let me know if this works or not, and please tell me the error message if not.
Kind regards,
u_fep
In your first script, you are looping through all players so it will add the coins value to each one
I think replacing the first script with this should work fine:
local StageCoinPlus = game.ReplicatedStorage:WaitForChild("StageCoinPlus")
local Players = game:GetService("Players")
local PlayerData = require(game.ServerScriptService.PlayerData:WaitForChild("Manager"))
StageCoinPlus.OnServerInvoke = function(player)
local profile = PlayerData.Profiles[player]
if not profile then return end
profile.Data.StageCoinsValue += 100
player.StatsFolder.StageCoinsValue.Value += 100
end
Another possible issue is that you’re firing the event in every client with the local player variable, so when other players touch the part the .Touched event is getting fired in every client as the local player. Try adding this line right below your Parted.Touched event:
local Parted = game.Workspace:WaitForChild("Parted")
local StageCoinPlus = game.ReplicatedStorage:WaitForChild("StageCoinPlus")
local player = game.Players.LocalPlayer
Parted.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if Parted.CanTouch == true then
Parted.CanTouch = false
StageCoinPlus:InvokeServer(player)
end
end
if hit.Parent ~= player.Character then return end
wait(5)
Parted.CanTouch = true
end)
local PlayerService = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Parted = workspace:WaitForChild("Parted", 300)
local StageCoinPlus = ReplicatedStorage:WaitForChild("StageCoinPlus", 300)
local LocalPlayer = PlayerService.LocalPlayer
Parted.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
local Player = PlayerService:GetPlayerFromCharacter(hit.Parent)
if Humanoid and Player and Player == LocalPlayer and Parted.CanTouch == true then
Parted.CanTouch = false
StageCoinPlus:InvokeServer(Player)
end
task.wait(5)
Parted.CanTouch = true
end)
Server Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local StageCoinPlus = ReplicatedStorage:WaitForChild("StageCoinPlus", 300)
local PlayerData = require(ServerScriptService.PlayerData:WaitForChild("Manager", 30))
StageCoinPlus.OnServerInvoke = function(player)
local profile = PlayerData.Profiles[player]
if not profile then return end
profile.Data.StageCoinsValue += 100
player.StatsFolder.StageCoinsValue.Value += 100
end
local Parted = game.Workspace:WaitForChild("Parted")
local StageCoinPlus = game.ReplicatedStorage:WaitForChild("StageCoinPlus")
local player = game.Players.LocalPlayer
Parted.Touched:Connect(function(hit)
if hit.Parent ~= player.Character then return end
if hit.Parent:FindFirstChild("Humanoid") then
if Parted.CanTouch == true then
Parted.CanTouch = false
StageCoinPlus:InvokeServer(player)
end
end
wait(5)
Parted.CanTouch = true
end)