Right now i am in the midst of working on a script which upon the enemy getting killed it will update the datastore based off a values table.
Every other script works except for this one:
local lasthitplr = ""
local tweenservice = game:GetService('TweenService')
local clone = script.Parent:Clone(' ')
local origposition = script.Parent:GetPrimaryPartCFrame()
local effect = false
local datastore2 = require(game.ServerScriptService.DataStore2)
local MainKey = "DataStoreKey"
for i, v in pairs (script.Parent:GetChildren()) do
if v:IsA('Part') then
v.Touched:Connect(function(hit)
if game.Players:FindFirstChild(hit.Parent.Parent.Name) then
if effect == false then
lasthitplr = hit.Parent.Parent.Name
end
end
end)
end
end
local function effect()
local userdata = datastore2(MainKey, game.Players[lasthitplr]):Get()
local Values = datastore2("Values", game.Players[lasthitplr])
effect = true
local char = game.Workspace:WaitForChild(lasthitplr)
local moneymin = tonumber((script.Parent.Humanoid.MaxHealth * 0.15) * game.ReplicatedStorage:WaitForChild(lasthitplr.."'s Values").CoinMultiplier.Value) --the minimum amount of money the enemy will drop
local expmin = tonumber((script.Parent.Humanoid.MaxHealth * 0.15))
local expgain = math.floor(math.random(expmin, expmin * 1.35))
local coingain = math.floor(math.random(moneymin, moneymin * 1.35))
for i = 1, 4 do
local partfx = Instance.new('Part')
partfx.Anchored = false
partfx.CanCollide = true
partfx.Transparency = 1
partfx.Size = Vector3.new(.1,0.1,.1)
partfx.Name = "coineffect"
partfx.Parent = script
partfx.Position = script.Parent.Head.Position + Vector3.new(0,1,0)
local fxclone = game.ReplicatedStorage.Effects.Coin:Clone()
fxclone.Parent = partfx
wait()
end
script.Parent = game.Workspace
wait(1)
for i, v in pairs(script:GetChildren()) do
v.Anchored = true
v.CanCollide = false
end
local parts = script:GetChildren()
for i = 0, 1, .01 do
parts[1].CFrame = parts[1].CFrame:Lerp(char.HumanoidRootPart.CFrame, i)
parts[2].CFrame = parts[2].CFrame:Lerp(char.HumanoidRootPart.CFrame, i)
parts[3].CFrame = parts[3].CFrame:Lerp(char.HumanoidRootPart.CFrame, i)
parts[4].CFrame = parts[4].CFrame:Lerp(char.HumanoidRootPart.CFrame, i)
wait()
for i, v in pairs(parts) do
v.Touched:Connect(function(hit)
if hit.Name == "HumanoidRootPart" then
v:Destroy()
end
end)
end
end
for i, v in pairs(script:GetChildren()) do
v:Destroy()
end
if game:GetService('MarketplaceService'):PlayerOwnsAsset(game.Players:FindFirstChild(lasthitplr), 7031512) then
userdata.Values.Experience = userdata.Values.Experience + expgain * 2
else
userdata.Values.Experience = userdata.Values.Experience + expgain
end
Values:Set(userdata.Values)
if game:GetService('MarketplaceService'):PlayerOwnsAsset(game.Players:FindFirstChild(lasthitplr), 7031507) then
userdata.Values.Coins = userdata.Values.Coins + coingain * 2
Values:Set(userdata.Values)
game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value = game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value - coingain * 2
for i = game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value, game.ReplicatedStorage:WaitForChild(lasthitplr.."'s Values").Coins.Value + (coingain * 2), coingain * .01 do
wait()
game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value = i
end
else
userdata.Values.Coins = userdata.Values.Coins + coingain
Values:Set(userdata.Values)
game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value = game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value - coingain
for i = game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value, game.ReplicatedStorage:WaitForChild(lasthitplr.."'s Values").Coins.Value + coingain, coingain * .01 do
wait()
game.ReplicatedStorage:FindFirstChild(lasthitplr.."'s Values").Coins.Value = i
end
end
script:Destroy()
end
script.Parent.Humanoid.Died:Connect(function()
repeat wait() until lasthitplr ~= ""
effect()
end)
i don’t know what to do, error is occuring on this line:
userdata.Values.Experience = userdata.Values.Experience + expgain
