I’ll try that now! I’ll let you know if it works.
uhhhhh
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if not Player:FindFirstChild("InBattle") then
local Bool = Instance.new("BoolValue", Player)
Bool.Name = "InBattle"
end
if not Player:FindFirstChild("Battling") then
local Obj1 = Instance.new("ObjectValue", Player)
Obj1.Name = "Battling"
end
if not Player:FindFirstChild("PlayerInventory_" .. tostring(Player.UserId)) then
local InventoryFolder = Instance.new("Folder", Player)
InventoryFolder.Name = "PlayerInventory_" .. tostring(Player.UserId)
end
local InventoryFolder = Player:FindFirstChild("PlayerInventory_" .. tostring(Player.UserId))
if not Player:FindFirstChild("PlayerItems") then
local ItemFolder = Instance.new("Folder", InventoryFolder)
ItemFolder.Name = "PlayerItems"
end
if not Player:FindFirstChild("PlayerWeapons") then
local WeaponFolder = Instance.new("Folder", InventoryFolder)
WeaponFolder.Name = "PlayerWeapons"
end
local ItemFolder = Player:FindFirstChild("PlayerItems")
local WeaponFolder = Player:FindFirstChild("PlayerWeapons")
local PlayerData = Player:WaitForChild("PlayerStatistics")
local PlayerItems = PlayerData:WaitForChild("Items")
local PlayerWeapons = PlayerData:WaitForChild("Weapons")
for _, Item in pairs(PlayerItems:GetChildren()) do
if Item:IsA("NumberValue") then
local AmountOfItems = Item.Value
for i=0, AmountOfItems do
local ItemToClone = Folder_Items:FindFirstChild(Item.Name):Clone()
ItemToClone.Parent = ItemFolder
ItemToClone.Name = ItemToClone.Name
task.wait(0.01)
i+=1
end
end
end
task.spawn(function()
while true do
for _, Item in pairs(ItemFolder:GetChildren()) do
if Item:IsA("IntValue") then
if Item.Value <= 0 then
Item:Destroy()
end
end
end
for _, Weapon in pairs(WeaponFolder:GetChildren()) do
if Weapon:IsA("IntValue") then
if Weapon.Value <= 0 then
Weapon:Destroy()
end
end
end
task.wait(1)
end
end)
end)
end)
Did you change anything in the script? If so, change it back and run it. Once you’re done, send me a screenshot of all the open folders.
I’ve just done the whole if not player thing and when I tested it this is what it looked like:
It works!
(however pressing this button)
leads to this:
You forgot to open up the Items, Statistics, and Weapons folder. Could you show me it?
To your refresh character problem, could you show me the MainBattler script?
Entire MainBattler script:
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local Debris = game:GetService("Debris")
local Folder_Default = workspace:WaitForChild("DefaultService")
local Folder_Assets = ReplicatedStorage:WaitForChild("Assets")
local Folder_Remotes = Folder_Assets:WaitForChild("Remotes")
local Folder_Models = Folder_Assets:WaitForChild("Models")
local Folder_Items = Folder_Models:WaitForChild("Items")
local Folder_Weapons = Folder_Models:WaitForChild("Weapons")
local MoveCaster = Folder_Remotes:WaitForChild("CastMove")
local Folder_Characters = Folder_Models:WaitForChild("Characters")
local Folder_Arenas = Folder_Models:WaitForChild("Arenas")
local NPCs = Folder_Default:WaitForChild("BattleNPCs")
local _debug = true
local MoveComplete = false
local AlreadyDone = false
local function findTarget(HRP, Range)
local players = Players:GetPlayers()
local maxDistance = Range
local nearestTarget
for i, player in pairs(players) do
if player.Character then
local target = player.Character
if target ~= nil and target.PrimaryPart ~= nil then
local distance = (HRP.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance then
nearestTarget = target
maxDistance = distance
end
end
end
end
print("RPG SYSTEM | Scanning.")
return nearestTarget
end
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if not Player:FindFirstChild("InBattle") then
local Bool = Instance.new("BoolValue", Player)
Bool.Name = "InBattle"
end
if not Player:FindFirstChild("Battling") then
local Obj1 = Instance.new("ObjectValue", Player)
Obj1.Name = "Battling"
end
if not Player:FindFirstChild("PlayerInventory_" .. tostring(Player.UserId)) then
local InventoryFolder = Instance.new("Folder", Player)
InventoryFolder.Name = "PlayerInventory_" .. tostring(Player.UserId)
end
local InventoryFolder = Player:FindFirstChild("PlayerInventory_" .. tostring(Player.UserId))
if not Player:FindFirstChild("PlayerItems") then
local ItemFolder = Instance.new("Folder", InventoryFolder)
ItemFolder.Name = "PlayerItems"
end
if not Player:FindFirstChild("PlayerWeapons") then
local WeaponFolder = Instance.new("Folder", InventoryFolder)
WeaponFolder.Name = "PlayerWeapons"
end
local ItemFolder = Player:FindFirstChild("PlayerItems")
local WeaponFolder = Player:FindFirstChild("PlayerWeapons")
local PlayerData = Player:WaitForChild("PlayerStatistics")
local PlayerItems = PlayerData:WaitForChild("Items")
local PlayerWeapons = PlayerData:WaitForChild("Weapons")
for _, Item in pairs(PlayerItems:GetChildren()) do
if Item:IsA("NumberValue") then
local AmountOfItems = Item.Value
for i=0, AmountOfItems do
local ItemToClone = Folder_Items:FindFirstChild(Item.Name):Clone()
ItemToClone.Parent = ItemFolder
ItemToClone.Name = ItemToClone.Name
task.wait(0.01)
i+=1
end
end
end
task.spawn(function()
while true do
for _, Item in pairs(ItemFolder:GetChildren()) do
if Item:IsA("IntValue") then
if Item.Value <= 0 then
Item:Destroy()
end
end
end
for _, Weapon in pairs(WeaponFolder:GetChildren()) do
if Weapon:IsA("IntValue") then
if Weapon.Value <= 0 then
Weapon:Destroy()
end
end
end
task.wait(1)
end
end)
end)
end)
for _, NPC in pairs(NPCs:GetChildren()) do
if NPC:IsA("Folder") then
if NPC:FindFirstChild("Stats") then
local NPC_Stats = require(NPC.Stats)
local NPC_Model = NPC:FindFirstChildOfClass("Model")
local HRP = NPC_Model.PrimaryPart
local Humanoid = NPC_Model:FindFirstChildOfClass("Humanoid")
local RangeTG = NPC_Stats.RangeToGrab
local RangeTF = NPC_Stats.RangeToFight
local Identifier = Instance.new("Part", HRP)
Identifier.Size = Vector3.new(RangeTG, RangeTG, RangeTG)
Identifier.Position = HRP.Position
Identifier.Name = "_Hitbox"
Identifier.Material = Enum.Material.Neon
Identifier.BrickColor = BrickColor.new("Lime green")
Identifier.Transparency = 1 --0.6
Identifier.CanCollide = false
local Weld = Instance.new("Weld", HRP)
Weld.Part0 = HRP
Weld.Part1 = Identifier
---NOW TO THE ACTUAL CHASING---
local BattleDebounce = false
local EnemyNPC = Folder_Characters:WaitForChild(NPC_Stats.Type):Clone()
task.spawn(function()
while task.wait(0.5) do
local Target = findTarget(HRP, RangeTF)
if Target and Target.Humanoid.Health > 0 then
local Player = Players:GetPlayerFromCharacter(Target)
if Player.InBattle.Value == true then
break
end
end
if Target and Target.Humanoid.Health > 0 then
repeat task.wait()
Humanoid:MoveTo(Target.PrimaryPart.Position)
until Humanoid.MoveToFinished:Wait()
Identifier.Touched:Connect(function(Hit)
if Players:GetPlayerFromCharacter(Hit.Parent) == Players:GetPlayerFromCharacter(Target) then
if not BattleDebounce then
print("Battle Starting!")
Identifier.CanTouch = false
Players:GetPlayerFromCharacter(Target).InBattle.Value = true
Humanoid.MoveToFinished:Wait()
HRP.Anchored = true
Folder_Remotes.MovementConfig:FireClient(Players:GetPlayerFromCharacter(Target), false)
NPC_Model.Head.Alert:Play()
NPC_Model.Head.AlertMarker.PE:Emit(1)
BattleDebounce = true
end
end
end)
end
end
local Target = findTarget(HRP, RangeTF)
local ArenaToClone = Folder_Arenas:WaitForChild(NPC_Stats.ArenaType):Clone()
local StartSound = Instance.new("Sound")
StartSound.Parent = Folder_Default.bin
StartSound.SoundId = "rbxassetid://" .. NPC_Stats.BattleSound
StartSound:Play()
StartSound.Volume = 0.3
Debris:AddItem(StartSound, 10)
Folder_Remotes.StartBattle:FireClient(
Players:GetPlayerFromCharacter(Target),
NPC_Stats.BattleImage,
NPC_Stats.MusicChoice,
NPC.Stats,
NPC_Stats.Type
)
ArenaToClone.Parent = workspace.CurrentArena
EnemyNPC.Parent = ArenaToClone.Enemies
EnemyNPC.PrimaryPart.CFrame = ArenaToClone.EnemySpawn.CFrame
Target.PrimaryPart.CFrame = ArenaToClone.PlayerSpawn.CFrame
local PhysLevel = Instance.new("IntValue", EnemyNPC.Config)
PhysLevel.Value = NPC_Stats.LVL
PhysLevel.Name = "Level"
Players:GetPlayerFromCharacter(Target).Battling.Value = NPC
task.spawn(function()
local CorrectAmounts = 0
for i=CorrectAmounts, 5, 1 do
Target.PrimaryPart.CFrame = ArenaToClone.PlayerSpawn.CFrame
task.wait(1)
end
end)
end)
end
end
end
MoveCaster.OnServerEvent:Connect(function(Player, Type, Item)
if Player and Player.Character then
if not MoveComplete then
local Arena = workspace.CurrentArena:FindFirstChildOfClass("Folder")
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
MoveComplete = true
local PlayerInv = Player:WaitForChild("PlayerInventory_" .. tostring(Player.UserId))
local PlrItems = PlayerInv:WaitForChild("PlayerItems")
local PlrWeapons = PlayerInv:WaitForChild("PlayerWeapons")
if Type == "Item" then
if not AlreadyDone then
AlreadyDone = true
print(Player.Name .. " has used: " .. Item .. "!")
local PhysicalItem = Folder_Items:FindFirstChild(Item)
if PhysicalItem then
Humanoid.Health += PhysicalItem.Heal.Value
PhysicalItem.Uses.Value -= 1
if PhysicalItem.Uses.Value == 0 then
PlrItems:FindFirstChild(PhysicalItem.Name):Destroy()
end
Folder_Remotes.MoveCompleted:FireClient(Player, "Item", "Players")
MoveComplete = false
task.wait(2.5)
AlreadyDone = false
Folder_Remotes.EnemyTurn:FireClient(Player)
end
end
elseif Type == "Attack" then
if not AlreadyDone then
AlreadyDone = true
print(Player.Name .. " has attacked the enemy using a " .. Item .. "!")
Humanoid:MoveTo(Arena.AttackArea.Position)
Humanoid.MoveToFinished:Wait()
local EnemyFolder = Arena:FindFirstChild("Enemies")
local Enemy = EnemyFolder:FindFirstChildOfClass("Model")
local EnemyHumanoid = Enemy:FindFirstChildOfClass("Humanoid")
--
local PhysicalItem = Folder_Weapons:FindFirstChild(Item)
local DamageValue = PhysicalItem:FindFirstChild("Damage")
local DurabilityValue = PhysicalItem:FindFirstChild("Durability")
local DecayValue = PhysicalItem:FindFirstChild("Decay")
EnemyHumanoid.Health -= DamageValue.Value
DurabilityValue.Value -= DecayValue.Value
local SFX = PhysicalItem.Handle.Hit:Clone()
SFX.Parent = Folder_Default.bin
SFX:Play()
Debris:AddItem(SFX, 5)
task.wait(1.5)
Humanoid:MoveTo(Arena.PlayerSpawn.Position)
Humanoid.MoveToFinished:Wait()
if EnemyHumanoid.Health <= 0 then
Character.PrimaryPart.CFrame = workspace.Stage.Stages[tostring(Player.leaderstats.Stage.Value)].CFrame
else
Character.PrimaryPart.CFrame = Arena.PlayerSpawn.CFrame
end
if EnemyHumanoid.Health <= 0 then
Folder_Remotes.MovementConfig:FireClient(Player, true)
Folder_Remotes.EndBattle:FireClient(Player)
Arena:Destroy()
Enemy:Destroy()
Player.InBattle.Value = false
MoveComplete = false
local ObjVal = Player.Battling.Value
ObjVal:Destroy()
for _, NPC in pairs(NPCs:GetChildren()) do
if NPC:IsA("Folder") then
local NPC_Model = NPC:FindFirstChildOfClass("Model")
local HRP = NPC_Model.PrimaryPart
if HRP:FindFirstChild("_HitBox") then
HRP._HitBox.CanTouch = true
HRP.Anchored = false
end
end
end
end
Folder_Remotes.MoveCompleted:FireClient(Player, "Attack", "Players")
MoveComplete = false
task.wait(2.5)
AlreadyDone = false
Folder_Remotes.EnemyTurn:FireClient(Player)
end
end
end
end
end)
Folder_Remotes.EnemyMove.OnServerEvent:Connect(function(Player, Type)
if Type == "Attack" then
if not workspace.CurrentArena:FindFirstChildOfClass("Folder") then return end
local Arena = workspace.CurrentArena:FindFirstChildOfClass("Folder")
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local EnemyFolder = Arena:FindFirstChild("Enemies")
local Enemy = EnemyFolder:FindFirstChildOfClass("Model")
local EnemyHumanoid = Enemy:FindFirstChildOfClass("Humanoid")
EnemyHumanoid:MoveTo(Arena.EnemyArea.Position)
EnemyHumanoid.MoveToFinished:Wait()
Humanoid:TakeDamage(Enemy.Config.Damage.Value * math.round(Enemy.Config.Level.Value / 0.5))
local SFX = Enemy.Config.Damage.AttackSound:Clone()
SFX.Parent = Folder_Default.bin
SFX:Play()
Debris:AddItem(SFX, 5)
task.wait(1)
EnemyHumanoid:MoveTo(Arena.EnemySpawn.Position)
EnemyHumanoid.MoveToFinished:Wait()
Enemy.PrimaryPart.CFrame = Arena.EnemySpawn.CFrame
AlreadyDone = false
Folder_Remotes.MoveCompleted:FireClient(Player, "Attack", "Enemies")
end
end)
The Open Folders: (without refresh)
Could you show me the line where the error is?
I fixed the issue with the 106 line error! It was just a simple check with it.
So everything works as intended now?
No. I’ll quickly do a re-play of it to check.
Any Ideas on how to prevent this duplicate issue?
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if not Player:FindFirstChild("InBattle") then
local Bool = Instance.new("BoolValue", Player)
Bool.Name = "InBattle"
end
if not Player:FindFirstChild("Battling") then
local Obj1 = Instance.new("ObjectValue", Player)
Obj1.Name = "Battling"
end
if not Player:FindFirstChild("PlayerInventory_" .. tostring(Player.UserId)) then
local InventoryFolder = Instance.new("Folder", Player)
InventoryFolder.Name = "PlayerInventory_" .. tostring(Player.UserId)
end
local InventoryFolder = Player:FindFirstChild("PlayerInventory_" .. tostring(Player.UserId))
if not Player:FindFirstChild("PlayerItems") then
local ItemFolder = Instance.new("Folder", InventoryFolder)
ItemFolder.Name = "PlayerItems"
end
if not Player:FindFirstChild("PlayerWeapons") then
local WeaponFolder = Instance.new("Folder", InventoryFolder)
WeaponFolder.Name = "PlayerWeapons"
end
local ItemFolder = Player:FindFirstChild("PlayerItems")
local WeaponFolder = Player:FindFirstChild("PlayerWeapons")
local PlayerData = Player:WaitForChild("PlayerStatistics")
local PlayerItems = PlayerData:WaitForChild("Items")
local PlayerWeapons = PlayerData:WaitForChild("Weapons")
for _, Item in pairs(PlayerItems:GetChildren()) do
if Item:IsA("NumberValue") then
local AmountOfItems = Item.Value
for i=0, AmountOfItems do
local ItemToClone = Folder_Items:FindFirstChild(Item.Name):Clone()
ItemToClone.Parent = ItemFolder
ItemToClone.Name = ItemToClone.Name
task.wait(0.01)
i+=1
end
end
end
task.spawn(function()
while true do
if Player:FindFirstChild("PlayerItems") then
for _, Item in pairs(ItemFolder:GetChildren()) do
if Item:IsA("IntValue") then
if Item.Value <= 0 then
Item:Destroy()
end
end
end
end
if Player:FindFirstChild("PlayerWeapons") then
for _, Weapon in pairs(WeaponFolder:GetChildren()) do
if Weapon:IsA("IntValue") then
if Weapon.Value <= 0 then
Weapon:Destroy()
end
end
end
end
task.wait(1)
end
end)
end)
end)
Try changing the if statements to be like this:
Old
New
Nice! All the folders are working but theres a weird issue since none of the physical items want to be spawned into the folders too…
What was the fix to the items not deleting? Can you show me that?
Uhhh. Did you mean that 106 error I was on about?
No. I mean the initial reason you opened this topic. You had 4 issues in total:
- The initial, where items weren’t destroyed at durability 0.
- 106 Error
- Duplicate Folders
- Physical Items not added to the folder
All issues are fixed except for #4 Correct? I want to see the fix for #1.
Also, show where the items are supposed to be added to the folder initially
Im currently trying to figure out how to add the items back in. I don’t know what I did but they don’t seem to clone into the physical folder. Here’s what I have been trying to do:
for _, ItemVal in pairs(PlayerItems:GetChildren()) do
if ItemVal:IsA("IntValue") then
for i=0, ItemVal.Value do
local ItemToFind = Folder_Items:FindFirstChild(ItemVal.Name)
local NewItem = ItemToFind:Clone()
NewItem.Parent = ItemFolder
i+=1
if i == ItemVal.Value then
break
end
end
end
end
EDIT: Nevermind! It was meant to be a number value.
Right, I’m getting close to getting it to work again. But I encountered a bit of a error…
for _, ItemVal in pairs(PlayerItems:GetChildren()) do
if ItemVal:IsA("NumberValue") then
for i=0, ItemVal.Value - 1 do
local ItemToFind = Folder_Items:FindFirstChild(ItemVal.Name)
local NewItem = ItemToFind:Clone()
NewItem.Parent = ItemFolder
i+=1
if i == ItemVal.Value then
break
end
end
end
end
for _, WeaponVal in pairs(PlayerWeapons:GetChildren()) do
if WeaponVal:IsA("NumberValue") then
for i=0, WeaponVal.Value - 1 do
local ItemToFind = Folder_Weapons:FindFirstChild(WeaponVal.Name)
local NewItem = ItemToFind:Clone()
NewItem.Parent = WeaponFolder
i+=1
if i == WeaponVal.Value then
break
end
end
end
end