Hello,
Apologies for how long this post is and how complicated the problem is. I wanted to include as much relevant information as I could to provide the clearest picture with the problem I’m having.
I have added Tools that are welded to Pets that can be summoned in my game, and have also set up IntValues inside the Datastore to represent stats for the Pets - PetLevel and PetEXP. I want to ensure that I have scripted the best methods for updating these IntValues.
Issue: My Datastore does not appear to want to update the PetLevel IntValue properly when the appropriate PetEXP value has been reached.
Process:
When a Player summons a Pet, it becomes attached to the Player’s UpperTorso as a Part, and that Part then has a Tool and Handle inside:
Inside the Tool (labeled ‘Pet’ also) here is the LocalScript - ‘HurtLocalScript’, which I have setup to award the PetEXP IntValue upon Touch of an opposing player:
local Player = game.Players.LocalPlayer
local damage = 100
local x = script.Parent.Weapon
x.Touched:Connect(function(x)
if x.Parent:FindFirstChild("Humanoid") and x.Parent.Name ~= Player.Name then
Player.leaderstats.PetEXP.Value = Player.leaderstats.PetEXP.Value + 1
wait()
end
end)
Inside the Handle of the Tool, labeled as as ‘Weapon’, is the Script - ‘HurtScript’, which I have setup to deal damage to the opposing player:
local Player = script.Parent.Parent.Parent.Parent.Parent
local damage = 100
local x = script.Parent.Parent.Weapon
if x.Parent:FindFirstChild("Humanoid") and x.Parent.Name ~= Player.Name then
x.Parent.Humanoid:TakeDamage(damage)
end
end)
As you can see in the script above, I may not be identifying the Player in the best way from the Script inside the Tool’s Handle. Ideally, I’d like to have this be one script, but it currently is applying damage to the opposing player, and it does update the PetEXP IntValue inside the player’s leaderstats. However, and I think this is due to the way I have set up the previous two scripts, my Datastore script does not do the math equation properly to convert the PetEXP into PetLevel values. This is the relevant section inside my Datastore Script:
local requiredPet = (PetLevel.Value*77)
PetEXP.Changed:Connect(function()
if PetEXP.Value >= requiredPet then
PetLevel.Value = PetLevel.Value + 1
PetEXP.Value = PetEXP.Value - requiredPet
end
end)
I know the math formula should work, as its the same formula used for the Player’s Level to increase after obtaining the required EXP, which works properly. From what I can tell, this is due to the way the weapon scripts inside the Tool attached to the Pet, which is welded to the Player’s UpperTorso is processing the request.
When I manually change the PetEXP value while inside Play mode, the PetLevel adjusts accordingly, but when I manually change the PetEXP during a Client test, the PetLevel does not adjust. Currently, the PetEXP.Value continues to grow larger and larger without converting as it should with the math equation in the Datastore.
I’ve attempted to modify a regular “sword” script from weapons that the player equips and place the Script “HurtScript” inside the Handle, but without luck:
local Player = script.Parent.Parent.Parent.Parent.Parent
local damage = 100
local tool = script.Parent.Parent
local debris = game:GetService("Debris")
local handle = tool:WaitForChild("Weapon")
local hithumanoids = {}
equipped = true
handle.Touched:connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
local TargetPlayer = game.Players:GetPlayerFromCharacter(humanoid.Parent)
if equipped and Player and humanoid and humanoid.Health > 0 and hit and not hit:isDescendantOf(Player) then
if humanoid and humanoid.Health > 0 and not hithumanoids[humanoid]
then
hithumanoids[ humanoid] = true
for _, v in pairs( humanoid:GetChildren()) do
if v and v.Name == "creator" then
v:remove()
end
end
local tag = Instance.new("ObjectValue")
tag.Name = "creator"
tag.Value = Player
debris:AddItem(tag, 3)
tag.Parent = humanoid
humanoid:TakeDamage(damage)
else
end
end
end
end)
Again, not sure this is the best way to identify the Player from the Script, but since its a Tool attached to a part inside the Player’s UpperTorso, I’m unsure of a better method. I was thinking that having a script similar to the Player’s weapons, would cooperate better with the “Death Script” I have that normally updates the Player’s leaderstats. Below is the edited, relevant, portion of my Death Script that updates the leaderstats inside the Datastore:
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(char)
local hum = char:WaitForChild('Humanoid')
hum.Died:connect(function()
local tag = hum:FindFirstChild('creator')
if tag ~= nil then
if tag.Value ~= nil then
local leaderstats = tag.Value:FindFirstChild('leaderstats')
if leaderstats ~= nil then
leaderstats.Coins.Value = leaderstats.Coins.Value + player.leaderstats.Level.Value * 2
leaderstats.Experience.Value = leaderstats.Experience.Value + player.leaderstats.Level.Value * 20
end
end
end
end)
end)
end)
Any advice or assistance would be much appreciated. Thank you.
UPDATE: Question was solved thanks to DarkContinuum and iRexBot. Thank you both!