#Local Part Spawn Collection System

1 messages · Page 1 of 1 (latest)

sullen crater
#

12:02:00.404 collectiblesFolder is not a valid member of Workspace "Workspace" - Server - CollectablesHandler:27

#

This is the script the error is coming from, Its a normal script inside ServerScriptService

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local Workspace = game:GetService("Workspace")

local CollectEvent = ReplicatedStorage.Events.CollectibleEvents.Collect
local GetSpawnPartEvent = ReplicatedStorage.Events.CollectibleEvents.GetSpawnPart

local RngClient = require(ServerScriptService.RngClient)

local SpawningPart = Workspace.SpawningPart


GetSpawnPartEvent.OnServerInvoke = function(player)
    print("Server Invoked for SpawnPartEvent")
    local Rng = RngClient.SelectRandom(player)

    local Color = Rng[3]
    local Value = Rng[4]
    local RandomPos = SpawningPart.Position + Vector3.new(math.random(-SpawningPart.Size.X/2, SpawningPart.Size.X/2),SpawningPart.Size.Y/2 + 1,math.random(-SpawningPart.Size.Z/2, SpawningPart.Size.Z/2))
    
    return {Color = Color,Value = Value,Position = RandomPos}
end

CollectEvent.OnServerEvent:Connect(function(player, part)
    print("Collect Event Recieved")
    if not part then warn("Error-1") return end
    if not part:IsDescendantOf(workspace.collectiblesFolder) then warn("Error0") return end

    local char = player.Character
    if not char then warn("Error1") return end
    local hrp = char:FindFirstChild("HumanoidRootPart")
    if not hrp then warn("Error2") return end

    local radius = player:GetAttribute("Radius") or 0
    if (part.Position - hrp.Position).Magnitude <= radius then

        local reward = part:GetAttribute("Value") or 1
        print(player.Name .. " collected a part worth " .. reward)

        local state = player:GetAttribute("Money")
        player:SetAttribute("Money", state + reward)

        part:Destroy()
    else
        warn("Error3")
    end
end)```
#

This is the script which creates the part located in StarterCharacterScripts and is a local script

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

local GetSpawnPartEvent = ReplicatedStorage.Events.CollectibleEvents.GetSpawnPart

local Player = Players.LocalPlayer

local CollectiblesFolder = Workspace:WaitForChild("collectiblesFolder")

local SpawnSpeed = nil


Player:GetAttributeChangedSignal("SpawnSpeed"):Connect(function()
    SpawnSpeed = Player:GetAttribute("SpawnSpeed")
    print("SpawnSpeed updated:", SpawnSpeed)
end)

local function SpawnCollectible()
    local Data = GetSpawnPartEvent:InvokeServer(Player)
    
    if Data then
        local Color = Data.Color
        local Value = Data.Value
        local Pos = Data.Position
        
        local part = Instance.new("Part")
        part.Size = Vector3.new(1,2,1)
        part.Anchored = true
        part.CanCollide = false
        part.Color = Color
        part:SetAttribute("Value", Value)
        part.Name = "Collectible"
        part.Parent = CollectiblesFolder
        part.Position = Pos
    else
        warn("No data was given")
    end
end


task.spawn(function()
    while true do
        SpawnCollectible()
        task.wait(SpawnSpeed)
    end
end)```
fair plover
# sullen crater This is the script which creates the part located in StarterCharacterScripts and...

not sure, but maybe is cuz part:IsDescendantOf(Workspace.collectiblesFolder) "part" doesnt exist, if "part" is this (code that u have in your local):
local part = Instance.new("Part")
part.Size = Vector3.new(1,2,1)
part.Anchored = true
part.CanCollide = false
part.Color = Color
part:SetAttribute("Value", Value)
part.Name = "Collectible"
part.Parent = CollectiblesFolder
part.Position = Pos

cuz u are creating that from the local script, so when the server try to find it, it simple doesnt found it

sullen crater
clear pebbleBOT
#

studio** You are now Level 16! **studio

fair plover
sullen crater
#

so i made some new code

#
local GetSpawnPart = ReplicatedStorage.Events.CollectibleEvents:WaitForChild("GetSpawnPart")

local SpawningPart = Workspace.SpawningPart

local function GetRandomData(player : Player)
    local Rng = RngClient.SelectRandom(player)

    local Color = Rng[3]
    local Value = Rng[4]
    local RandomPos = SpawningPart.Position + Vector3.new(math.random(-SpawningPart.Size.X/2, SpawningPart.Size.X/2),SpawningPart.Size.Y/2 + 1,math.random(-SpawningPart.Size.Z/2, SpawningPart.Size.Z/2))
    
    return {Color = Color,Value = Value,Position = RandomPos}
end

local function CreatePart(Data, Folder)
    if Data then
        local Color = Data.Color
        local Value = Data.Value
        local Pos = Data.Position

        local part = Instance.new("Part")
        part.Size = Vector3.new(1,2,1)
        part.Anchored = true
        part.CanCollide = false
        part.Color = Color
        part:SetAttribute("Value", Value)
        part.Name = "Collectible"
        part.Parent = Folder
        part.Position = Pos
        
        part.Transparency = 1
        part.CanCollide = false
        
        return part
    else
        warn("No data was given")
    end
end

local function SpawnSet(player : Player)
    local SpawnTime = player:GetAttribute("SpawnSpeed")
    local CollectiblesFolder = Workspace:WaitForChild("Collectables_" .. player.Name)


    local Data = GetRandomData(player)
    local Part = CreatePart(Data, CollectiblesFolder)
    
    if Part then
        GetSpawnPart:InvokeClient(player, Part)
    else
        warn("Varible Part is nil")
    end

    task.wait(SpawnTime)
    SpawnSet(player)
end

Players.PlayerAdded:Connect(function(player)
    local PlayerFolder = Instance.new("Folder")
    PlayerFolder.Name = "Collectables_" .. player.Name
    PlayerFolder.Parent = Workspace
    
    SpawnSet(player)
end)```
#

This is what spawns the parts on the server, i had to remove the varibles at the top so i could send it

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

local GetSpawnPartEvent = ReplicatedStorage.Events.CollectibleEvents.GetSpawnPart

local Player = Players.LocalPlayer


GetSpawnPartEvent.OnClientInvoke = function(part : Part)
    if part then
        print("Client Invoked for SpawnPartEvent")
        
        part.Transparency = 0
        part.CanCollide = true
    else
        warn("Part was given as nil")
    end
end```
Then this is the local script for the player
#

I get the error that Part was given as nil @fair plover on the client side and idk how to fix it

sullen crater
#

and each part spawned will need to be different for each player due to different values like spawn speed values rng factors e.g

fair plover
#

oh

#

the part has to be create in the client side then

#

hm

sullen crater
fair plover
sullen crater
#

because if you can not make parts on the server how would you even verify them?

fair plover
sullen crater
#

but yeah thank you trying to help it means a lot

#

if you get any ideas feel free to let me know

fair plover
sullen crater
boreal harness
#

like setting attribute

sullen crater
#

that would be a good idea to optimise it

boreal harness
sullen crater
#

but i dont think it helps get over my issue first

#

of even creating the parts

boreal harness
#

Just learn to script

sullen crater
#

im just asking for some help

boreal harness
#

Yeah but you dont know collection service and you put modules in serverscriptservice (Mostly in replicated storage but its fine too)
Ok so if its me then I would just send the spawn location to the server using remote function and then let the server create the part and then return the part and make it only visible to the player.

sullen crater
#

I tried that above

fair plover
#

@sullen crater i made it work

sullen crater
fair plover
sullen crater
#

and parts spawn just for the player?

fair plover
#

yep

sullen crater
fair plover
# sullen crater what did you use?

local spawnSite = workspace:WaitForChild("World"):WaitForChild("SpawnSite")
local coin = game:GetService("ReplicatedStorage"):WaitForChild("Coin")
local coinModule = require(script.Parent:WaitForChild("CoinsModule"))
local zonePosition = spawnSite.Position - Vector3.new(spawnSite.Size.X/2,0,spawnSite.Size.Z/2 )

while task.wait(2) do
if coinModule.actualCoins < coinModule.maxCoins then
local coinClone = coin:Clone()
coinClone.Parent = workspace
coinClone.Position = zonePosition + Vector3.new(math.random(2, spawnSite.Size.X), 2, math.random(2, spawnSite.Size.Z) )
end
end

#

this just for the spawn

#

i used a module script to handle how much coins i have and whats the max can i create

#

and used collection service for the coin thing

sullen crater
fair plover
#

im just using that 3 scripts

sullen crater
fair plover
sullen crater
#

ahh right

sullen crater
#

actually i can do it

#

thank you so much

#

your a life saver

sullen crater
fair plover
#

player

sullen crater
#

to handle the addition of coins

fair plover
sullen crater
#

and make it safe

fair plover
#

:FireServer

sullen crater
#

how do i prevent a exploiter from just fireing it

#

so like what checks do i do before i just give the player the rewards

boreal harness
sullen crater
fair plover
sullen crater
# fair plover

when you used touch is there a way to check if another part touchs it

#

not me

#

beacuse i was gonna use this radius to collect them

fair plover
#

how long have u been scripting?

sullen crater
sullen crater
#

i started in 2022

fair plover
# sullen crater how did you do that

just as i told u:
collection:GetInstanceAddedSignal("coin"):Connect(function(part)
addedEvent:FireServer()

part.Touched:Once(function(hit)
    collectEvent:FireServer()

    
    part:Destroy()

end)

end)

fair plover
# sullen crater how did you do that

-- and on server side:

local addedEvent = game:GetService("ReplicatedStorage"):WaitForChild("Event"):WaitForChild("added")
local updateEvent = game:GetService("ReplicatedStorage"):WaitForChild("Event"):WaitForChild("update")
local collectEvent = game:GetService("ReplicatedStorage"):WaitForChild("Event"):WaitForChild("collect")

addedEvent.OnServerEvent:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
local coinsModule = require(char:WaitForChild("CoinsModule"))
local textLabel = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("Frame"):WaitForChild("TextLabel")
coinsModule.actualCoins = coinsModule.actualCoins + 1

if coinsModule.actualCoins > coinsModule.maxCoins then
    print("Hacking")
end
updateEvent:FireClient(player, coinsModule.actualCoins, coinsModule.maxCoins)

end)

collectEvent.OnServerEvent:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
local coinsModule = require(char:WaitForChild("CoinsModule"))
local textLabel = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("Frame"):WaitForChild("TextLabel")
coinsModule.actualCoins = coinsModule.actualCoins - 1

if coinsModule.actualCoins > coinsModule.maxCoins or coinsModule.actualCoins < 0 then
    print("Hacking")
end
updateEvent:FireClient(player, coinsModule.actualCoins, coinsModule.maxCoins)

end)

sullen crater
#

what is the update event for?

fair plover
#

updateEvent.OnClientEvent:Connect(function(actual, max)
coinModule.actualCoins = actual
coinModule.maxCoins = max
textLabel.Text = tostring(actual .."/".. max)

local player = game.Players.LocalPlayer
local textLabel = player.PlayerGui.ScreenGui.Frame.TextLabel
textLabel.Text = tostring(actual.."/"..max)

end)