How to score when killing an NPC - lua

When I kill an NPC, my score should increase, but it does not.
Here's my code. It is located under my NPC model.
local Humanoid = script.Parent.Humanoid
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
Leaderstats.Score.Value = Leaderstats.Score.Value + 250 --Change Money to the stat that is increased.
wait(0.1)
script:remove()
end
end
end
end
Humanoid.Died:connect(PwntX_X)
And here's my code for the leaderboard
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("BoolValue",plr)
stats.Name = "leaderstats"
local score = Instance.new("IntValue", stats)
score.Name = "Score"
score.Value = 0
end)

Not sure, but looks like
score.Value = Leaderstats.Score.Value

Related

How to create a DataStore?

local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')
-- Erstellt Leaderboard
local function onPlayerJoin(player)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local clicks = Instance.new('IntValue')
clicks.Name = 'Clicks'
clicks.Value = 0
clicks.Parent = leaderstats
local rebirths = Instance.new('IntValue')
rebirths.Name = 'Rebirths'
rebirths.Value = 0
rebirths.Parent = leaderstats
-- Wenn ein Nutzer verlassen hat das die daten gespeichert wurden
local playerUserId = 'Player_'..player.UserId
local data = playerData:GetAsync(playerUserId)
if data then
clicks.Value = data['Clicks']
rebirths.Value = data['Rebirths']
else -- Wenn user das erste mal joint das value 0 ist
clicks.Value = 0
rebirths.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function ()
local playerUserId = 'Player_'..player.UserId
playerData:SetAsync(playerUserId, player_stats)
end)
if not success then
warn('Could not save data')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
I tried to make a DataStore that stores the values of the leaderboard into a data that returns everytime if i rejoin. But it doesnt work and idk why. Im kinda new into lua programming but u guys can help me :D thanks.

My weapon only works on NPC's and not players

So basically I made a weapon it flings people after you hit them but it only works on NPC's and not players. Could someone explain to me why this is?
local power = 5
local function fling(character)
local humanoid = character:FindFirstChildOfClass("Humanoid")
local rootpart
if humanoid then
humanoid:ChangeState(11)
rootpart = humanoid.RootPart
end
if not rootpart then
rootpart = character.AssemblyRootPart
end
if rootpart then
rootpart.Velocity = Vector3.new(math.random(-100, 100), math.random(50, 100), math.random(-100, 100)) * 10
end
end
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local slash = script:WaitForChild("Slash")
local debounce = false
local idle = false
local PlayersHit = {}
local playerService = game:GetService("Players")
local plr = playerService:GetPlayerFromCharacter(tool.Parent)
local Datastore2 = require(game.ServerScriptService:WaitForChild("Modules").Datastore2)
local defaulthits = 0
local hitds = Datastore2("hits",plr)
tool.Activated:Connect(function()
if idle == false and debounce == false then
debounce = true
local humanoid = tool.Parent:WaitForChild("Humanoid")
local animtrack = humanoid:LoadAnimation(slash)
animtrack:Play()
wait(1)
debounce = false
idle = false
end
end)
handle.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= tool.Parent then
if debounce == true and PlayersHit[hit.Parent] == nil then
fling(hit.Parent)
hitds:Increment(1, defaulthits)
debounce = false
script.Parent.Handle.hit:Play()
end
end
end)
This is my current code. I do not know what to do I'm pretty sure its something to do with the fling script.
I found out how instead of humanoid:changestate(11) i used humanoid.PlatformStand = true

Part doesn't gives damage with Touched Event

I made a code for my game, it's a type of punch, with a hitbox made with a common Part. I proggrammed to the part give some damage according with the player strenght. It didn't work, no errors in output. Code here:
local rep = game:GetService("ReplicatedStorage")
local debounceDMG = true
rep.Combate.Soco.OnServerEvent:Connect(function(plr)
local math = math.random(1,2)
local char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = char.Humanoid
local lanim = char.Humanoid:LoadAnimation(script.Left)
local lanim1 = char.Humanoid:LoadAnimation(script.Animation)
local hitbox = Instance.new("Part")
hitbox.Parent = workspace
hitbox.Transparency = 1
hitbox.Anchored = true
hitbox.CanCollide = false
hitbox.Size = Vector3.new(2.5,2.5,2.5)
game:GetService("RunService").Heartbeat:Connect(function()
if math == 2 then
hitbox.Position = char.LeftHand.Position
elseif math == 1 then
hitbox.Position = char.RightHand.Position
end
end)
if math == 2 then
lanim:Play()
elseif math == 1 then
lanim1:Play()
end
hitbox.Touched:Connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h then
if h ~= Humanoid and debounceDMG then
debounceDMG = false
h.Health -= plr.Data.Forca.Value + 5
task.wait(.5)
debounceDMG = true
end
end
end)
task.wait(.3)
hitbox:Destroy()
end)
You use "if h~=Humanoid" will always look for Non Humanoid, your code will never reach the Health deduction. Try to code below if working
hitbox.Touched:Connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil and debounceDMG then
debounceDMG = false
h.Health -= plr.Data.Forca.Value + 5
task.wait(.5)
debounceDMG = true
end
end)

attempt to index nil with "clicks" fix?

here it is, it is roblox code:
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("AddStats")
local player = game:GetService("Players")
local leaderstats = player:WaitForChild('leaderstats', 10)
local clicks = leaderstats.Clicks
local rebirths = leaderstats.Rebirths
local function clicksStore()
clicks.Value = clicks.Value + 1
if rebirths <= 1 then
clicks.Value = clicks.Value + rebirths.Value
end
end
remoteEvent.OnServerEvent:Connect(clicksStore)
is there anyway i can fix this? its getting on my nerves. Thank you for helping!
You are indexing the leaderstats correctly, but in a server Script, you aren't accessing the player correctly.
You can easily access the player by using the RemoteEvent's OnServerEvent connection.
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("AddStats")
local function clicksStore(player)
local leaderstats = player.leaderstats
local clicks = leaderstats.Clicks
local rebirths = leaderstats.Rebirths
local clickValue = 1 + rebirths.Value
clicks.Value += clickValue
end
remoteEvent.OnServerEvent:Connect(clicksStore)

Roblox autosave leadboard but save only one Value

I tried to make the functions via Datastore so that it only saved the data Wins and reset the stage always but saved Wins always so if I have 250 stages and 1 win and I leave the game I will have 0 stages and 1 win.
local CheckpointService = {}
-- SERVICES
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
-- MODULES
local AchievementService = require(game.ServerScriptService.Server:WaitForChild("Achievement"))
-- VARIABLES
local CHECKPOINTS = game.Workspace:WaitForChild("Checkpoints")
local STAT_NAME = "Stage"
local WINS_NAME = "Wins"
local PREVENT_SKIPPING = false
-- CONNECTIONS
local function playerAdded(player)
local leaderstats = Instance.new("Folder", player) or player:FindFirstChild("leaderstats")
leaderstats.Name = "leaderstats"
local checkpointStat = Instance.new("IntValue", leaderstats)
checkpointStat.Name = STAT_NAME
checkpointStat.Value = 1
local checkpointStat = Instance.new("IntValue", leaderstats)
checkpointStat.Name = WINS_NAME
checkpointStat.Value = 0
player.CharacterAdded:connect(function(character)
repeat
wait()
until CHECKPOINTS and checkpointStat
local goto = CHECKPOINTS:FindFirstChild("Checkpoint " .. checkpointStat.Value)
if goto then
character:MoveTo(goto.Position)
else
warn("[SERVER] - Checkpoint " .. checkpointStat.Value .. " not found!")
end
end)
end

Resources