Resource/item respawn

  1. I’m stumped. I have figured out spawning randomly, but when I destroy a resource, it does not want to respawn. I have tried so many variations

I have looked all over the place for a solution and no such luck for what I want to accomplish.

I have simplified this code to not include random positions, etc. Any input thoughts, links, etc? I really need help understanding this concept. I have tried while true loops, etc. DM me if you are willing to assist over voice so I can understand this way better.

This block of code is in my server script service

replicatedStorage = game:GetService("ReplicatedStorage")

local StoneFolder = Instance.new("Folder")
StoneFolder.Name = "StoneFolder"
StoneFolder.Parent = workspace
local stoneIntValue = Instance.new("IntValue")
stoneIntValue.Name = "stoneIntValue"
stoneIntValue.Parent = StoneFolder
stoneIntValue.Value = 0


local minX = -15
local maxX = 5
local minZ = 0
local maxZ = 20






local stone = replicatedStorage.Ore.Stone

local numOfStones = 0

local function spawnStoneOre()
	local clone = stone:Clone()
	clone.Parent = workspace.StoneFolder
	clone.Position = Vector3.new(0,0,0)
	
end


spawnStoneOre()

stoneIntValue.Changed:Connect(function()
		print("IT WORKED")
	end)




This block of code is for my pickaxe


local tool = script.Parent
local canMine = false
local Value = 0
local PickDamage = 10
local Type = "Pickaxe"
local destroyed = game.ReplicatedStorage.Ore.Stone.Destroyed
destroyed = false
local StoneIntValue = game.Workspace.StoneFolder.stoneIntValue


local function onTouch(otherPart)
	script.Parent.Activated:Connect(function(Click)
	if otherPart.Name == "Stone" and canMine then
		canMine = false
			otherPart.Health.Value = otherPart.Health.Value - PickDamage
			game.Players.LocalPlayer.Stats.Stone.Value = game.Players.LocalPlayer.Stats.Stone.Value + 1
			print("+1 Stone")
			if destroyed == false and otherPart.Health.Value == "0" then
				otherPart:Destroy()
				StoneIntValue.Value = StoneIntValue.Value - 1
				end
	elseif otherPart.Name == "Iron" and canMine then
		game.Players.LocalPlayer.PlayerGui.MiningError.Enabled = true
		wait(4)
		game.Players.LocalPlayer.PlayerGui.MiningError.Enabled = false
	elseif otherPart.Name == "Copper" and canMine then
		canMine = false
		game.Players.LocalPlayer.PlayerGui.MiningError.Enabled = true
		wait(4)
		game.Players.LocalPlayer.PlayerGui.MiningError.Enabled = false
	elseif otherPart.Name == "Gold" and canMine then
		canMine = false
		game.Players.LocalPlayer.PlayerGui.MiningError.Enabled = true
		wait(4)
		game.Players.LocalPlayer.PlayerGui.MiningError.Enabled = false
	elseif otherPart.Name == "Diamond" and canMine then
		canMine = false
		game.Players.LocalPlayer.PlayerGui.MiningError.Enabled = true
		wait(4)
		game.Players.LocalPlayer.PlayerGui.MiningError.Enabled = false
		end
end)
end

local function slash()
	local str = Instance.new("StringValue")
	str.Name = "toolanim"
	str.Value = "Slash" 
	str.Parent = tool
	canMine = true
end
1 Like

You’re only calling the spawnStoneOre() method once in your first code block. Moreover, any of the code on the client (second block) that modifies values created on the server (first block) will not replicate to the server.

You will need to create a RemoteEvent that gets fired when the client destroys a block that the server then handles (that is, giving the item to the player, respawning the resource, etc.)

1 Like

Yeah, I’m just trying to figure out the best way of doing this. I did all the spawnStoneOre() function where the print was. While running the game, I see the IntValue there changing, but does not fire the changed event.

I will try messing around with the remote events.

Thank you for your input

Yes, the IntValue will change on the client end, but if you switch to the server view of the game, it will not have changed. The LocalScript running on the client cannot directly change the value on the server, but it can change the value on the client. Because the .Changed event is connected on the server and nothing has technically changed for it, it does not fire.

1 Like

Got that part figured out. Thank you :slight_smile: