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)
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.
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)
-- 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
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().
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