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)
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
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.
wait, i did get a error 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.
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.
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.
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)
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)