How to make multiple items work on one script

I want to duplicate these currency’s but still want the script to work. I tried putting the script inside of the object and using

script.Parent

But I couldn’t do that because I need to access a value inside the player which I only know how to do in a local script.
Here is the code I am using inside a local script inside starter player scripts:

local plr = script.Parent.Parent
local tix = game:GetService("Workspace"):WaitForChild("Tix")
local value = game.Players.LocalPlayer.PlayerScripts.Money
local gui = game.Players.LocalPlayer.PlayerGui["Money GUI"].Frame.Number
local money = plr.PlayerScripts.Money

value.Changed:Connect(function(newValue)
	gui.Text = tostring(newValue)
end)


tix.ProximityPrompt.Triggered:Connect(function(plr)
	money.Value = money.Value + 1
	tix.Transparency = 1
	tix.CanCollide = false
	tix.Decal.Transparency = 1
	tix.ProximityPrompt.Enabled = false
	game.ReplicatedStorage.Money:Play()

end)

local robux = game:GetService("Workspace"):WaitForChild("Robux")

robux.ProximityPrompt.Triggered:Connect(function(plr)
	money.Value = money.Value + 5
	robux.Transparency = 1
	robux.CanCollide = false
	robux.Decal.Transparency = 1
	robux.ProximityPrompt.Enabled = false
	game.ReplicatedStorage.Money:Play()

end)

well u can store players in a table when they join the game and then get the name or id of that player and do whatever u want

I tried that but playerscripts isn’t a member of player added. Could you be more specific?

i dont understand clearly what do u want to acheive

I want to create a script for a collectable currency, but the one am I currently using breaks if I duplicate the currency

If i understood you right… Your code should work if you do this:

Paste this script into your ServerScriptService

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

local updateCurrencyEvent = Instance.new("RemoteEvent")
updateCurrencyEvent.Name = "UpdateCurrency"
updateCurrencyEvent.Parent = ReplicatedStorage

-- or just make this event in ReplicatedStorage by yourself 

local function onCurrencyCollected(player, amount)
    local money = player.leaderstats:FindFirstChild("Money") 
    if money then
        money.Value = money.Value + amount
        updateCurrencyEvent:FireClient(player, money.Value)
    end
end

local tixFolder = game.Workspace.tixFolder -- Put all your tix models in this folder

for _, tix in pairs(tixFolder:GetChildren()) do 
tix.ProximityPrompt.Triggered:Connect(function(player)
    onCurrencyCollected(player, 1)
    tix.Transparency = 1
    tix.CanCollide = false
    tix.Decal.Transparency = 1
    tix.ProximityPrompt.Enabled = false
    ReplicatedStorage.Money:Play()
end)
end

-- If you have any other currency then do the same

Also put this local script in StarterPlayerScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local gui = player.PlayerGui["Money GUI"].Frame.Number

local updateCurrencyEvent = ReplicatedStorage.UpdateCurrency

updateCurrencyEvent.OnClientEvent:Connect(function(newValue)
    gui.Text = tostring(newValue)
end)

Explanation:

Server Script: handles the currency collection and updates the player’s currency value. It also fires a RemoteEvent to update the GUI on the client side. Oh and it’s makes it so every model in your folder listens for the prox event. Less lags and better flexibility. But i recommend you watching some videos about this topic

Local Script: listens for the RemoteEvent and updates the GUI with the new currency value. Change gui name if i misspelled it. I don’t know if any bug can appear, but it shouldn’t. And I don’t recommend using you :WaitForChild if you don’t know how to use it. I didn’t change your code, only fixed :grinning:

1 Like

Hey thanks for the help, just one error that I don’t know how to fix.
In this line it expects brackets inside the brackets somewhere, I think?

for _, tix in pairs(tixFolder:GetChildren) do

The text in the bracket is underlined and the output says expected “(” or “{” when parsing function call got “)”

Oh my bad, i forgot to paste () after GetChildren so we didn’t call a function. If any other error occurs then it’s my blindless but they should be easy to fix. Reply me if need

Ah yes one more very strange error, I just tested it and found out that playerscripts under my player is completely gone. It’s just not there, I don’t know how or why. Was this intended?

I tested it again and it is there, maybe I am blind, but it says in the output that it isn’t a member of my player? Oh, sorry the reason it wasn’t there was because I was viewing from the server, but the error still occurs.

Actually, sorry for the multiple posts but I don’t think player scripts is a member because the script is on the server side.

Can you explain if money value is? Like send me some screenshots of where’s your stuff so i can understand. Error occurs because i don’t really understand where’s money is. And yeah, unlike the Backpack and PlayerGui containers, the PlayerScripts container is not accessible to the server. Server Script objects will not run when parented to PlayerScripts.

Its inside playerscripts, which seems weird, but I tried putting it straight inside the player and it just didn’t work, should I move it?
image

Ok, look. If you want for money to be a thing, you have to create a leaderstats. There’s a quick code for you how you can do this. Create a new script in ServerScriptService and put this code in it:

-- Script inside ServerScriptService

game.Players.PlayerAdded:Connect(function(player)
    -- Create leaderstats folder
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    -- Create Money value
    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = 0 -- Starting amount of money
    money.Parent = leaderstats
end)

I have also changed the main code so you can see how it works. What you should know: Use only leaderstats name for this folder so roblox will automatically initialize it. You can check how it works by pressing the f5 button (or just test play your game)

Okay thanks, I thought it wasn’t necessary but thanks. However, there is still an error saying that playerscripts is not a valid member of my player on the server script.

Check my main (first code) that i have sent to you. You should change in function onCurrencyCollected

local money = player.leaderstats.Money, not PlayerScripts

Awesome! Thanks for all the help, I am pretty new, and it was appreciated. This almost turned into a tutorial for others :smile:

I hope it helped! Keep learning :wink:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.