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

NumVal isn't working for object respawning script?

Asked by 4 years ago

So I'm making a game that has an auto spawner for items, kind of like what simulators use. I'm trying to add "1" to a NumVal everytime a "Trash" (what I'm calling the item) spawns. I also have a script inside "Trash" that removes 1 from the NumVal before it gets collected. My problem is that the 1 isn't getting removed from the NumVal, therefore, "Trash" won't spawn.

Here is the trash spawner script inside ServerScriptService:

local TrashSpawnArea = workspace.TrashSpawnArea
local Trash = workspace.Trash
local r = 50
local numval = script.Parent.TrashSpawnScript.AmountOfTrash
local numval0 = numval.Value

while wait(1) do
    if numval0 <15 then
        local TrashClone = Trash:Clone()
        local x,z = TrashSpawnArea.Position.X, TrashSpawnArea.Position.Z
        x,z = math.random(x - r, x + r), math.random(z - r, z + r)
        local pos = Vector3.new(x, TrashSpawnArea.Position.Y, z)
        TrashClone.Parent = workspace
        TrashClone.Parent = workspace.TrashDuplicates
        TrashClone.Position = pos
        numval0 = numval0 + 1
    end
end

This script works fine on It's own but when I try to remove 1 from the value, it doesn't work because the items don't spawn again.

Here is the script inside the object (Trash):

local CleaningSmoke = script.Parent.CleaningSmoke
local Trash = script.Parent
local ClickDetector = script.Parent.ClickDetector
local numval = game.ServerScriptService.TrashSpawnScript.AmountOfTrash.Value

ClickDetector.MouseClick:Connect(function()
    CleaningSmoke.Enabled = true
    wait(2.5)
    numval = numval - 1
    wait(2.5)
    Trash:Destroy()
end)

I'm really struggling with this, It's my first time actually scripting a game. Any help is appreciated!

Answer this question