Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

3 comments

0

tag.Value is not a player's character

0

Enemy is the humanoid

0

Ik, enemy is humanoid.

2 answers

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.

2 comments

0

this means that "_M", "Mob" and "Enemy" can't be local

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.