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

EXP for kill not working. Can someone take a look at it for me and see where I've make a mistake?

Okay, so I'm somewhat new to scripting but I'm not a total "noob" at it. This script is placed inside my NPC and when it's killed it's supposed to give the player that killed the NPC EXP or double EXP depending upon if they own the game pass or not. Currently the whole script doesn't work can someone help me? So I have a similar script inside the NPC that also gives the player a "Kill" (This script works perfectly) but I'm pretty sure I broke the script when I was trying to add the game pass part onto it.

Code that's giving me trouble:

local Humanoid = script.Parent.Humanoid -- NPC Humanoid Name
function PwntX_X() 
local tag = Humanoid:findFirstChild("creator") 
	if tag ~= nil then 
		if tag.Value ~= nil then 
local Leaderstats = tag.Value:findFirstChild("leaderstats") 
			if Leaderstats ~= nil then 
amount = 50
id = 118972469
for i,v in pairs(game.Players:GetPlayers()) do
	if v then
		if game:GetService("MarketplaceService"):PlayerOwnsAsset(v,id) then
			v.leaderstats.EXP.Value = v.leaderstats.EXP.Value + amount * 2
		else
			v.leaderstats.EXP.Value = v.leaderstats.EXP.Value + amount	
wait(0.1)
script:remove()
			end 
		end 
	end 
end 
Humanoid.Died:connect(PwntX_X) 

Not sure if this is relevant, but the script that this code is based off of:

local Humanoid = script.Parent.Humanoid -- NPC Humanoid Name
function PwntX_X() 
local tag = Humanoid:findFirstChild("creator") 
	if tag ~= nil then 
		if tag.Value ~= nil then 
local Leaderstats = tag.Value:findFirstChild("PlayerStats") 
			if Leaderstats ~= nil then 
Leaderstats.Kills.Value = Leaderstats.Kills.Value + 1 
wait(0.1)
script:remove()
			end 
		end 
	end 
end 
Humanoid.Died:connect(PwntX_X) 

1 answer

Open up the Script Analysis window and it will tell you what sorts of syntax errors you have. The Output window is also important; it tells you what errors your scritps run into during run-time.

In general, you should use print statements to help determine what's going wrong (only when you have something specific you're testing for, though -- don't put them everywhere or it'll be too cluttered for when you do have a bug). What they print will show up in the Output and you'll be able to determine the values of variables/functions, which code blocks are running (or aren't), etc. ex, say you have an if statement that you think isn't working, if var > 0 and var2 < 10 then, right before the if block you might put print(var, var2, var > 0, var2 < 10). If the print doesn't even appear, the problem is before; if it prints "3, 7, true, true", then the if statement is working, it's something else. But if it says "3, 15, true, false", then you know the problem is before the if statement, something with var2.

On to your situation (read the comments):

--ModuleScript for DoubleEXP record-keeping:
local module = {}
local passId = 118972469
local GamePassService = game:GetService("GamePassService")
local playerHasDoubleEXP = {} --Dictionary<player, bool>
game.Players.PlayerAdded:Connect(function(player) -- Find out if they have the pass
    playerHasDoubleEXP[player] = GamePassService:PlayerHasPass(player, passId)
end)
game.Players.PlayerRemoving:Connect(function(player) -- Clean up memory
    playerHasDoubleEXP[player] = nil
end)
function module.PlayerHasDoubleEXP(player)
    return playerHasDoubleEXP[player]
end
function module.RecordPlayerPurchasedDoubleEXP(player) --TODO call this from where-ever you record this purchase (if you sell game passes in-game)
    playerHasDoubleEXP[player] = true
end
return module
--End of ModuleScript
--Alternatively, instead of using the ModuleScript, *if* you don't sell the game-pass in-game and don't mind having your players have to reload to gain its beneficial effects, you can just do this:
local passId = 118972469
local GamePassService = game:GetService("GamePassService") -- You have to use this service for Game Passes, not the MarketplaceService. You can read up on the function you *were* using here: http://wiki.roblox.com/index.php?title=API:Class/MarketplaceService/PlayerOwnsAsset    and the GamePassService here: http://wiki.roblox.com/index.php?title=API:Class/GamePassService 
function PlayerHasDoubleEXP(player)
    return GamePassService:PlayerHasPass(player, passId)
end
--If you use the 2nd option, you can keep that in your NPC script or put it in a ModuleScript - whichever you prefer. Either way, now to use it.
--I will assume that you put it in a ModuleScript: "game.ReplicatedStorage.PlayerHasDoubleEXP"

local PlayerHasDoubleEXP = require(game.ReplicatedStorage.PlayerHasDoubleEXP)
local Humanoid = script.Parent.Humanoid -- NPC Humanoid Name
local exp = 50
function PwntX_X() 
    local tag = Humanoid:FindFirstChild("creator") --Note: "findFirstChild" is deprecated
    if not tag or not tag.Value then print("No tag") return end -- This "if not tag" is an alternative style to what you had; this prevents having lots of indents
    local leaderstats = tag.Value:FindFirstChild("leaderstats")
    if not leaderstats or not leaderstats.Value then print("No leaderstats") return end -- Do make sure you are generating an ObjectValue called "leaderstats" as a child to the "creator" tag
    local exp = leaderstats.Value.EXP
    local playerHasPass = PlayerHasDoubleEXP.PlayerHasDoubleEXP(leaderstats.Parent)
    exp.Value = exp.Value + (playerHasPass and amount * 2 or amount) --[[This is identical to:
    if playerHasPass then
        exp.Value = exp.Value + amount * 2
    else
        exp.Value = exp.Value + amount
    end]]
    script:Destroy() -- :remove() and :Remove() are both deprecated
end
Humanoid.Died:connect(PwntX_X)

If in the output you see "No tag" then you have an additional problem -- the script you edited relies on a "tagging" system, where weapons will "tag" whomever they damage so that you know who killed who, and the weapons you're using don't do this. You'll have to fix the weapons or invent another way to determine who killed the NPC.

Similarly, if you see "No leaderstats", you haven't configured those weapons correctly. Possibly the line should be local leaderstats = tag.Value:FindFirstChild("PlayerStats"), like the original.

2 comments

0

Thank you for helping me. However, I'm still a bit confused as to where to place the script. Also, isn't the code source that you've provided meant to be broken up into separate scripts in different places. I'm sorry but I'm really not the best scripter and I'm still learning.

0

You are correct - if you look at the comments carefully, they were meant to be the guide on how to break it up. Lines 2-18 are a ModuleScript, up to line 25 is some alternate code for that ModuleScript, line 27 tells you where to put that ModuleScript, and the rest replaces the code you gave in the question.

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