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
I am lazy to explain :)
There were some probably with your first script, and I fixed it:
local flr = Instance.new("Folder", game.Workspace) flr.Name = "Coins" local Spawns = { Vector3.new(-20.192,2.943,1.5), Vector3.new(-20.192,2.943,-5.5), Vector3.new(-20.192,2.943,-11.5), Vector3.new(-20.192,2.943,-17.5), Vector3.new(-20.192,2.943,-23.5) } game.Players.PlayerAdded:Connect(function() for num,pos in pairs(Spawns) do --Define variables. local coin = game.ReplicatedStorage.Coin:Clone() coin.Parent = flr coin.Anchored = true coin.Name = "Coin"..num --Spawn coin. coin.Position = pos end end)
and your second one, also fixed:
while wait() do local Coins = game.Workspace:WaitForChild("Coins"):GetChildren() for i,v in pairs(Coins) do v.Touched:Connect(function(part) if part.Parent:FindFirstChild("Humanoid") then local thisplr = game.Players:FindFirstChild(part.Parent.Name) if thisplr then local stats = thisplr:FindFirstChild("leaderstats") if stats then local score = stats:FindFirstChild("Cash") if score then print("work") end end end v:Destroy() end end) end end