Coins update for all players when only one player collects the coin, if local script, the script doesnt work

Hey so basically in my game, Coins update for all players when only one player collects the coin, i want to fix this bug. i tried a localscript, but when i do a localscript, the script doesnt work. it might be something to do with the text script (text script is local script btw, i tried a normal script but it gives me no errors both ways.), Im not sure where this error is though. Probably from the Ui text update script because the intvalue called coins value is updating, but not the text.

UI Text Script:

local Coins = workspace.Coins

function Coins()
	script.Parent.Text = "Coins: " .. Coins.Value
end

Coins.Changed:Connect(Coins)

just a careless error in my code

3 Likes

You have nothing to differentiate who’s coin value that is. You don’t ever use the player variable, and you’re updating a single coin value that looks to be shared in the workspace.

Create different values for each individual player, and store them somewhere that makes it easy to make sure that it is their individual Coin value.

Example

game.Players.PlayerAdded:Connect(function(player)
     local coinvalue = Instance.new("IntValue")
     coinvalue.Name = "Coins"
     coinvalue.Parent = player
end)

This creates a new Coin value inside the Player object, and you would just search for it like this

local Coins = player:FindFirstChild("Coins")
Coins.Value = 1
4 Likes

dont really understand it, i changed it up a bit and i dont understand what your saying, maybe give a more detailed description?

You can’t just copy and paste the examples. You’ll need to change your code to use individual values instead of using the global one you already have in workspace.

2 Likes

wait, i did get a error image probably because i didnt add the workspace, and when i did it didnt work but no errors just like the first reply i made, i was refering to that, i did not copy the example. I changed it up a bit.

1 Like

what do you mean by individual values? do you mean i have to add a coin value? i already have one, or do you mean i have to make 10 coin values and add them in a folder? i dont really understand

The reason you’re having your problem is because every player is sharing the exact same coin value, instead of having their own. So yes, you have to create an individual value for each player. The code snippet I provided will create a new value when a player joins the game, so long as that snippet is inside a Script.

2 Likes

Okay, i see, ill try that. Question, is the example you told me (yes ik its an example, i will change it) suppose to be in the same script as the coin touched script, or is suppose to be in a seperate script.

1 Like

As long as it’s in a server script, it will run properly.

2 Likes

So the coin touched script? or are they two seperate scripts? also i did something like this? is this right?

game.Players.PlayerAdded:Connect(function(player)
     local coinvalue = Instance.new("IntValue")
     coinvalue.Name = "Coins"
     coinvalue.Parent = player
	 local Coins = player:FindFirstChild("Coins")
	 local db = true
	 function onTouch(hit)
		if hit.Parent:FindFirstChild("Humanoid") ~= nil then
			if db == true then
				db = false
				script.Parent.Transparency = 1
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				workspace.Coins.Value = workspace.Coins.Value + 1	
			end
		end
	end)
end)
1 Like

No, you’re doing it wrong you need to use 2 separate scripts the first script will setup players leaderstats and the second script will give the player some coins when they touch the part.

Stats script

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
     local StatsFolder = Instance.new("Folder")
     StatsFolder.Name = "leaderstats"
     StatsFolder.Parent = Player
	 
	Instance.new("IntValue",StatsFolder).Name = "Coins"
end)

Give the player a coin

local Players = game:GetService("Players")
local Sp = script.Parent

Sp.Touched:Connect(function(Object)
	local Player = Players:GetPlayerFromCharacter(Object.Parent)
	
	if (Player) then
		Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value + 1
		Sp:Destroy()
	end
	
end)

The first script needs to be located in ServerScriptService and the second script needs to be located in the part.

Also if you want to display the coins on a textlabel use this code

wait(1 + math.random())

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local LocalPlayer = Players.LocalPlayer
local leaderstats = LocalPlayer.leaderstats
local Coins = leaderstats.Coins
local Sp = script.Parent

RunService.Heartbeat:Connect(function()
	Sp.Text = "Coins: "..Coins.Value
end)
1 Like

Question, is there a way to do this script without the leaderstats? Like it will still show the coins even with the leaderstats

Remove the stats folder and insert the intvalue to player

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Instance.new("IntValue",Player).Name = "Coins"
end)

But you have to do some changes to other scrpts as well

2 Likes

Okay, What changes do i make to them?

1 Like

Instead of having Player.leaderstats.Coins you need to have Player.Coins

2 Likes

Thanks alot, it worked, i have been trying to fix this for 1 hour lol

1 Like

No problem make sure to make it as a solution.

3 Likes

i did mark it as a solution :wink:

2 Likes