Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Mob wont give a reward after being defeated, what's wrong?

Asked by 6 years ago
Edited 6 years ago
local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
Enemy.Died:Connect(function()
    if (not Enemy:FindFirstChild("creator")) then return end
    local tag = Enemy.creator
    local player = game.Players:GetPlayerFromCharacter(tag.Value)
    local Stats = player:WaitForChild("leaderstats")
    local XP = Stats:WaitForChild("XP")
    local Gold = Stats:WaitForChild("Gold")
    XP.Value = XP.Value + _M.XPAward
    Gold.Value = Gold.Value + _M.GoldAward
end)

the error in output:

Workspace.Bald Gang Member(lvl 5).RewardScript:8: attempt to index local 'player' (a nil value)
0
tag.Value is not a player's character Amiaa16 3227 — 6y
0
Enemy is the humanoid HaveASip 494 — 6y
0
Ik, enemy is humanoid. 666U5erName666 2 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

This answer is REALLY simple:

a) tag.Value is not a Character. The parameter of GetPlayerFromCharacter() must be a Character.

b) Local variables, when used outside of the scope they're defined in, have no value.

0
this means that "_M", "Mob" and "Enemy" can't be local DeceptiveCaster 3761 — 6y
0
What do you mean by that, as far as I can tell he used require correctly? From my understanding he is just using a module so he won't have to keep writing the same functions in each script. KardashevScale 110 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

If I'm not mistaken, you're using the method that Linked swords use to change values right? If that's the case, then you can't call GetPlayerFromCharacter() on an Object Value because an Object Value is not the character, in that instance it only stores the value of the player. Your error is caused because if the GetPlayerFromCharacter() function can't find a player, then it returns nil instead. A better way to get the player from the Object Value is just to do this: local player = tag.Value then change the stats from there.

Answer this question