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

How to make it to where I get XP when getting a kill with KeyBinded Punch? (not a tool)

(I'm using a basic RPG leaderboard, the XP on Kill script that the NPCs have match the leaderboard name.)

-- This is the Local Script in StarterCharacterScripts

local UserInputService = game:GetService("UserInputService") local ReplicatedStorage = game:GetService("ReplicatedStorage") local punchEvent = ReplicatedStorage:WaitForChild("PunchEvent")

local ready = true

local function punch(inputObject, gameProcessed) if inputObject.KeyCode == Enum.KeyCode.Q and ready then punchEvent:FireServer() ready = false wait(0.5) ready = true end end

UserInputService.InputBegan:Connect(punch)


-- ServerScript for Punch

math.randomseed(tick())

local ReplicatedStorage = game:GetService("ReplicatedStorage") local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage) punchEvent.Name = "PunchEvent"

local animations = {02910014105, 02910014105}

local function onPunchFired(plr) local char = game.Workspace:FindFirstChild(plr.Name) local humanoid = char.Humanoid local animation = Instance.new("Animation") local picked = math.random(1, #animations) animation.AnimationId = "http://roblox.com/asset/?id="..animations[picked] local animTrack = humanoid:LoadAnimation(animation) animTrack:Play() local dmgScript = script.DmgScript:Clone() if picked == 1 then dmgScript.Parent = char.RightHand elseif picked == 2 then dmgScript.Parent = char.LeftHand end dmgScript.Disabled = false wait(0.4) dmgScript:Destroy() end

punchEvent.OnServerEvent:Connect(onPunchFired)

-- Damage Script with boolvalue inside of the ServerScript

script.Parent.Touched:Connect(function(hit) local char = hit.Parent local hum = char:FindFirstChild("Humanoid") if hum and char.Name ~= script.Parent.Parent.Name then hum.Health = hum.Health - script.Dmg.Value script.Disabled = true wait(0.5) script.Disabled = false end end)

1 answer

✓ Accepted answer

What you could do is check if hum's health is going to equal to or be less than 0 after taking damage.

script.Parent.Touched:Connect(function(hit)
     local char = hit.Parent;
    local hum = char:FindFirstChildOfClass("Humanoid")
     if hum and char.Name ~= script.Parent.Parent.Name  do
       if(hum.Health - script.Dmg.Value) <= 0 then
         print(char.Name, "killed by", script.Parent.Parent.Name);
         -- give EXP here
       end
     hum:TakeDamage(script.Dmg.Value);
    end
end)

You basically check if they die after you hit them, and if they do, give the EXP, then deal the damage.

If you would like each NPC to give different EXP, you would add a NumberValue to each NPC and name it EXP. Then change it's value to how much EXP you want it to give. In the script below, the default is 50 if EXP doesn't exist. After that, do this:

script.Parent.Touched:Connect(function(hit)
     local char = hit.Parent;
    local hum = char:FindFirstChildOfClass("Humanoid")
     if hum and char.Name ~= script.Parent.Parent.Name  do
       if(hum.Health - script.Dmg.Value) <= 0 then
         local exp = char:FindFirstChild("EXP").Value or 50; -- Change 50 to the default if the NumberValue doesn't exist.
         print(char.Name, "killed by", script.Parent.Parent.Name);
         local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent); -- get the player instance
         -- give EXP. IDK where your EXP is, or if it's in a folder, so do that.
       end
     hum:TakeDamage(script.Dmg.Value);
    end
end)

6 comments

0

I made a spelling error while typing it and fixed it.

0

Thanks for the reply! How would I go about making specific NPCs give a certain amoubt of XP though?

0

I just updated my answer.

0

Just wondering, but why'd you unmark as answer?

0

Oh I accepted it without reading what you said first, sorry lol. In this case do I use a creator value to give the EXP for a kill?

0

Nope, plr is who killed the NPC/other player.

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