I have some questions for a game that I'm making and I wanted to put all of them in one place so it may be a bit messy. I'm a beginner at lua so I may have some dumb mistakes. If you misunderstood something tell me and I'll try to explain it better.
I'm making an obby game. I want to add coins around the map that when players collects them and get some cash. To spawn the coins I got a coin model in the Replicated Storage and when a player joins I use a function in local script in the StarterGui to Clone the coin.
local flr = Instance.new("Folder", game.Workspace)
flr.Name = "Coins"
function SpawnCoins(pos, num)
--Define variables.
local coin = game.ReplicatedStorage.Coin:Clone()
coin.Parent = flr
coin.Anchored = true
coin.Name = "Coin"..num
--Spawn coin.
coin.Position = pos
end
game.Players.PlayerAdded:Connect(SpawnCoins(Vector3.new(-20.192,2.943,1.5),1))
game.Players.PlayerAdded:Connect(SpawnCoins(Vector3.new(-20.192,2.943,-5.5),2))
game.Players.PlayerAdded:Connect(SpawnCoins(Vector3.new(-20.192,2.943,-11.5),3))
game.Players.PlayerAdded:Connect(SpawnCoins(Vector3.new(-20.192,2.943,-17.5),4))
game.Players.PlayerAdded:Connect(SpawnCoins(Vector3.new(-20.192,2.943,-23.5),5))
When the game starts it shows me an error for each line of:
game.Players.PlayerAdded:Connect(SpawnCoins(Vector3.new(x,y,z)
in the Output: Attempt to connect failed: Passed value is not a function
It still works but I would like to fix it.
Since I'm using Local Scripts to control the coins (spinning and floating) the scripts are in the StarterGui. Because of it I have to create the scripts for each coin because I have multiple models called "Coin" and one function can't handle all of them (as far as I know). To fix the problem I'm giving each coin a number (Coin1, Coin2 etc...), so I need different script for each coin. **My question is if there's a faster way to do this? **
In order to make it that when a player collects a coin it will be removed for him but not for the other players I've enabled Filtering and I'm creating the gold coins using a Local Script in the StartGui.
I've got a problem, when a player collects a coin the coin disappears for all the players. (I've tested it in a local server with 2 Players)
This is the code that removes the coin and call the remote function to give the money, Most of the code down here wasn't wrote by me, it's from a coin model from the assets store (It's in a local script):
local coin = game.Workspace:WaitForChild("Coins"):WaitForChild("Coin1")
function onTouched(part)
local h = part.Parent:findFirstChild("Humanoid")
if (h~=nil) then
local thisplr = game.Players:findFirstChild(h.Parent.Name)
if (thisplr~=nil) then
local stats = thisplr:findFirstChild("leaderstats")
if (stats~=nil) then
local score = stats:findFirstChild("Cash")
if (score~=nil) then
print("work")
end
end
end
coin:Remove()
end
end
coin.Touched:connect(onTouched)
Another problem I have is that when I call the Remote Function (when I've used print() it worked) the script doesn't remove the coin.
Thanks ahead

2 comments
Could you please organize your issue into one paragraph?
What’s with wrapping your if statements in brackets and the deprecated code?