Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Coin spawn system not detecting '.Touched' ?

Asked by 3 years ago
Edited 3 years ago

So basically, I have seen a few coin spawn systems and decided to make one of my own where all coin spawn, giving money to the player, etc can be controlled by ONE SCRIPT and not each script for each coin.

I have made 2 folders inside workspace namely - Coins and Spawns

There are 2 coin models in ServerStorage called Coin1 and Coin2

I also created a table in that script to insert or remove that are available to spawn a coin. I did this to avoid multiple coins spawning in 1 spawn at a time. I'm not sure if that's the right way to copy a table though, might have to change it later.

Coins is an empty folder which later contains all coin clones when the script runs Spawns is a folder containing certain parts on baseplate where the coin could spawn (though the coin is offset by a bit)

-- Vars
local spawns = workspace.Spawns
local coins = workspace.Coins

local coin1 = game.ServerStorage.Coin1
local coin2 = game.ServerStorage.Coin2

local spawnOffset = Vector3.new(math.random(-5, 5), 2, math.random(-5, 5))

local spawnsTable = spawns:GetChildren()
---------------------------------------------------
-- Functions

local function coinSpawn(coin)
    if #spawnsTable == 0 then return end

    local chosenSpawn = math.random(1, #spawnsTable)

    print(spawnsTable[chosenSpawn])

    local _coin = coin:Clone()
    _coin.Parent = coins
    _coin.Position = spawnsTable[chosenSpawn].Position + spawnOffset

    table.remove(spawnsTable, chosenSpawn)
end

---------------------------------------------------

while wait(math.random(1, 5))do
    if math.random(1, 3) == 3 then
        coinSpawn(coin2)
    else
        coinSpawn(coin1)
    end
end

for _, coin in pairs(coins:GetChildren()) do
    coin.Touched:Connect(function(hit)
        print(hit)
    end)
end

This script is located in ServerScriptService and I haven't made the destruction part yet

The problem is: it doesn't print the 'hit' part, which means this stops me from further continuing the script

I suspect that the problem is either in Roblox which fails to detect the collision or coins:GetChildren() is not updated when the script runs

Could someone help pls

0
I edited my code, maybe it might help~ BestCreativeBoy 1395 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The problem is that, you have code below while loop. And you might know, that while loop continues indefinitely, so the code after it never executes. To fix it, just put your for loop above the while loop.

for _, coin in ipairs(coins:GetChildren()) do
    coin.Touched:Connect(function(hit)
        print(hit)
    end)
end

while wait(math.random(1, 5))do
    if math.random(1, 3) == 3 then
        coinSpawn(coin2)
    else
        coinSpawn(coin1)
    end
end

Lemme know if it helps!

EDIT : I tried replicating your code, and finally made a working version of it. In your function, you just need to add :

_coin.Touched:Connect(function(hit)
    print(hit)
end)

table.remove(YourTable, Value)

It will work everytime, whenever, the cloned coin is Touched.

0
you're right, im dumb to do that......... but it still dosent work for me @BestCreativeBoy zeusplayz2005 5 — 3y
0
OMGGGGG IT WORKS THX i love you (*no homo) but could you explain the logic lol zeusplayz2005 5 — 3y
0
Well, here you are creating a variable for each cloned coin, so instead of looping through the coins and checking the Touched event, you can directly check it via the variable you created. BestCreativeBoy 1395 — 3y
Ad

Answer this question