• Lua Help
    4 replies, posted
Hi there, I'm currently in the process of making a simple mining system where you hit the rock and it will remove itself and drop an ore. The problem i'm having is that I want the rock to replace itself after 30 seconds but I can't manage to do this without re-spawning all rocks in the game. This is the code I have so far. function ENT:RespawningRock() local spawnPos = self:GetPos() self:Remove() local ore = ents.Create( table.Random( Ores ) ) ore:SetPos(spawnPos) ore:Spawn() timer.Create( "UniqueName3", Sleepy_RockRegenTime, 1, SpawnRocks ) end function SpawnRocks() local StoredRockData = {} if file.Exists( "sleepy_mining/" .. game.GetMap() .. ".txt" ,"DATA") then StoredRockData = util.JSONToTable(file.Read( "sleepy_mining/" .. game.GetMap() .. ".txt" )) end for i, v in pairs(  ents.FindByClass( "sleepy_rock" ) ) do       if( IsValid( v ) ) then         v:Remove()       end     end for k,v in pairs(StoredRockData) do local SleepyRock = ents.Create("sleepy_rock") SleepyRock:SetPos(v.Pos) SleepyRock:SetAngles(v.Angle) SleepyRock:Spawn() end end concommand.Add("spawnrocks", function(ply,cmd,args) SpawnRocks() end) Obviously this is going to remove all rocks as I've set it to as a workaround but does anybody have a solution to only re-spawn the missing rock? Thank you for your help.
The issue I can maybe see occurring is that you are trying to run code after calling self:Remove() which may prevent execution. You could try using a GM:EntityRemoved() hook to check when a "sleepy_rock" is about to be removed, get it's position and then spawn a new one there after a timer. hook.Add("EntityRemoved", "SleepyEntityRemoved", function(ent) if ent:GetClass() != "sleepy_rock" then return end local pos = ent:GetPos() local ang = ent:GetAngles() timer.Simple(Sleepy_RockRegenTime, function() local rock = ents.Create("sleepy_rock") rock:SetPos(pos) rock:SetAngles(ang) rock:Spawn() end end) This is untested but something like that should help you get towards the desired result.
This would be a hacky as fuck way of doing shit, but in the rock's OnRemove function, set a variable on the ore entity containing the rock's position vector. Then, in the ore's Use function, spawn a rock at the stored vector after a timer or whatever.
Exactly what I was looking for! Thanks alot for your help Got it, apologies.
Sorry, you need to Log In to post a reply to this thread.