[SOLVED] How to Make a Randomized Item Spawner

I have made the actual corresponding System of the “RandomizedItemSpawner” however I am struggling to make it that when the Player would to achieve the “Item” it will go into their inventory and the Item will spawn at a different location.

Here is the RandomizedItemSpawner Script:

-- BatteryLocation Script
local folder = workspace.Map.Zones.BatteryZone
local tool = game.ServerStorage.Battery

local toolSpawns = folder:GetChildren()
local clone = tool:Clone()
clone.CFrame = toolSpawns[math.random(1,#toolSpawns)].CFrame + Vector3.new(2,1,0) -- Picks one random Spawns CFrame with Y offset of 1 stud.
tool.Anchored = false
wait (2)
tool.Anchored = true
tool.Orientation = Vector3.new (0, 0, 90)
clone.Parent = workspace
tool.Parent = workspace

Here is the Item Script:

-- EToPickUp Script
local ToolNames = {"Battery"} -- Change Item to your item name and move it to ServerStorage
local Storage = game.ServerStorage.RareItems
local Battery = game.Workspace.Battery


local Part = script.Parent
local ProximityPrompt = Part:WaitForChild("ProximityPrompt")

ProximityPrompt.Triggered:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool then
				if Backpack:FindFirstChild(Tool.Name) == nil then
					Tool:clone().Parent = Backpack
					Battery:Destroy()
				end	
			end
		end
	end
end)
8 Likes

Any idea on how I am going to do this?

3 Likes

If you want to make the battery spawn at a different location after the player’s picked it up, you’ll need to use RemoteEvents or BindableEvents.

If you’re using a LocalScript for the pickup detection, which it looks like you aren’t, you’d use a RemoteEvent.

In this case, however, as you’re not using a LocalScript, it’d be something like this:

--// Code for the player picking up battery
game:GetService("ServerStorage").BatteryPickup:Fire()
game:GetService("ServerStorage").BatteryPickup.Event:Connect(batteryRespawn)

In this example, you use a BindableEvent to tell the script that spawns the battery to spawn it again when the player has picked it up. The batteryRespawn function would be the function containing the code to be ran when the battery is picked up.

3 Likes

Hey sorry to ask but how exactly will I make that part of the System?

2 Likes

The first line of code I provided would go in the script you wrote that triggers when the battery is picked it up. You’d implement after the final if statement where you check to make sure the player doesn’t have a battery.

local Tool = Storage:FindFirstChild(ToolNames[i])
if Tool then
	if Backpack:FindFirstChild(Tool.Name) == nil then
        game:GetService("ServerStorage").BatteryPickup:Fire()
		Tool:clone().Parent = Backpack
		Battery:Destroy()
	end	
end

The second bit of code would go in the script you have for spawning the battery. You’d create a new function titled batteryRespawn that you call when the event is fired.

function batteryRespawn()
    --// The code to respawn the battery would go here.
end

game:GetService("ServerStorage").BatteryPickup.Event:Connect(batteryRespawn)
3 Likes

ETopickUp Script:

-- EToPickUp Script
local ToolNames = {"Battery"} -- Change Item to your item name and move it to ServerStorage
local Storage = game.ServerStorage.RareItems
local Battery = game.Workspace.Battery


local Part = script.Parent
local ProximityPrompt = Part:WaitForChild("ProximityPrompt")
local highlight = script:WaitForChild("Highlight")


ProximityPrompt.Triggered:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		for i = 1, #ToolNames do
			local Tool = Storage:FindFirstChild(ToolNames[i])
			if Tool then
				if Backpack:FindFirstChild(Tool.Name) == nil then
					game:GetService("ServerStorage").BatteryPickup:Fire()
					Tool:clone().Parent = Backpack
					Battery:Destroy()
				end	
			end
		end
	end
end)

BatteryLocation Script:

local folder = workspace.Map.Zones.BatteryZone
local tool = game.ServerStorage.Battery

function batteryRespawn()
	--// The code to respawn the battery would go here.
end

game:GetService("ServerStorage").BatteryPickup.Event:Connect(batteryRespawn)

local toolSpawns = folder:GetChildren()
local clone = tool:Clone()
clone.CFrame = toolSpawns[math.random(1,#toolSpawns)].CFrame + Vector3.new(2,1,0) -- Picks one random Spawns CFrame with Y offset of 1 stud.
tool.Anchored = false
wait (2)
tool.Anchored = true
tool.Orientation = Vector3.new (0, 0, 90)
clone.Parent = workspace
tool.Parent = workspace
3 Likes

@LVSKCNBL is this looking correct to its extent?

3 Likes

Yes, that looks correct. The only thing you’d need to add in is the code making the battery respawn.

3 Likes

I just got this error – 04:48:12.708 BatteryPickup is not a valid member of ServerStorage "ServerStorage" - Server - BatteryLocation:8

2 Likes

The BatteryPickup is a BindableEvent you need to insert into ServerStorage and name.

3 Likes
local folder = workspace.Map.Zones.BatteryZone
local tool = game.ServerStorage.Battery

function batteryRespawn()
	tool = batteryRespawn()
end

--> 
![Screenshot 2023-12-04 045153|247x213](upload://pIzn6dA3jKDosz1Kw6YgMhPcUmF.png)
game:GetService("ServerStorage").BatteryPickUp.BatteryRespawn:Connect(batteryRespawn)

local toolSpawns = folder:GetChildren()
local clone = tool:Clone()
clone.CFrame = toolSpawns[math.random(1,#toolSpawns)].CFrame + Vector3.new(2,1,0) -- Picks one random Spawns CFrame with Y offset of 1 stud.
tool.Anchored = false
wait (2)
tool.Anchored = true
tool.Orientation = Vector3.new (0, 0, 90)
clone.Parent = workspace
tool.Parent = workspace
1 Like

Screenshot 2023-12-04 045153

error – 04:50:32.769 Connect is not a valid member of BindableEvent "ServerStorage.BatteryPickUp.BatteryRespawn" - Server - BatteryLocation:8

Do you think it will be easier if I gave you the Kit?

You’re going to need to rename the event from BatteryRespawn to BatteryPickUp and either specify the new directory you created by adding BatteryPickUp.BatteryPickUp or, if you want to change the name of the directory, to BatteryPickUp.BatteryRespawn. I’d recommend making sure the EToPickUp script has the new directory.

Further, under the batteryRespawn() function, you’re calling the function twice, which will throw an error as that will loop continuously, so remove tool = batteryRespawn().

1 Like
local folder = workspace.Map.Zones.BatteryZone
local tool = game.ServerStorage.Battery

function batteryRespawn()
	local clone = tool:Clone()
    clone.CFrame = toolSpawns[math.random(1,#toolSpawns)].CFrame + 
    Vector3.new(2,1,0) -- Picks one random Spawns CFrame with Y offset of 1 stud.
    tool.Anchored = false
    wait(2)
    tool.Anchored = true
    tool.Orientation = Vector3.new (0, 0, 90)
    clone.Parent = workspace
    tool.Parent = workspace
end

game:GetService("ServerStorage").BatteryPickUp.BatteryRespawn:Connect(batteryRespawn)

local toolSpawns = folder:GetChildren()
local clone = tool:Clone()
clone.CFrame = toolSpawns[math.random(1,#toolSpawns)].CFrame + Vector3.new(2,1,0) -- Picks one random Spawns CFrame with Y offset of 1 stud.
tool.Anchored = false
wait(2)
tool.Anchored = true
tool.Orientation = Vector3.new (0, 0, 90)
clone.Parent = workspace
tool.Parent = workspace

sorry to say but it is still giving me this error – 04:58:18.923 Connect is not a valid member of BindableEvent "ServerStorage.BatteryPickUp.BatteryRespawn" - Server - BatteryLocation:17

The reasoning behind the error – game:GetService("ServerStorage").BatteryPickUp.BatteryRespawn:Connect(batteryRespawn)

That’s because after .BatteryRespawn you forgot to put .Event, which is what is needed to connect the function.

1 Like

I have got another problem I can only Pick (Up) the Battery that was from ServerStorage and cannot Pick (Up) the Battery from the SpawnLocation.