Roblox - attempt to index nil with 'leaderstats' - lua

Can someone tell me how can I fix this error that shows up when I run my script? Thanks
line 4: Workspace.Slide1.PointsPart.Script:4: attempt to index nil with 'leaderstats'
script.Parent.Touched:Connect(function(hit)
local player = hit.Parent:FindFirstChild("Humanoid")
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr.leaderstats.Points.Value >= 0 then
wait()
script.Disabled = true
script.Parent.Transparency = 1
script.Parent.CanCollide = false
plr.leaderstats.Points.Value = plr.leaderstats.Points.Value +5
wait(0.5)
script.Parent.Transparency = 1
script.Parent.CanCollide = false
script.Disabled = false
end
end)

The Touched event fires for anything that touches the part. You are not handling the case that a part isn't a child of a Player's Character.
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if not plr then
return
end
if plr.leaderstats.Points.Value >= 0 then

You could change it to this
script.Parent.Touched:Connect(function(hit)
local player = hit.Parent:FindFirstChild("Humanoid")
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and plr.leaderstats.Points.Value >= 0 then
wait()
script.Disabled = true
script.Parent.Transparency = 1
script.Parent.CanCollide = false
plr.leaderstats.Points.Value = plr.leaderstats.Points.Value +5
wait(0.5)
script.Parent.Transparency = 1
script.Parent.CanCollide = false
script.Disabled = false
end
end)
the compiler will calculate the instance "plr" first,
if it isn't nil, then the compiler will calculate "plr.leaderstats.Points.Value >= 0"
and this process called "Short-Circuit Evaluation"

Related

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)

Two Codes Confliting each other, how to fix it?

I made 2 codes of special abilitys and if i activate the SuperFireSphere code, then the Combat code damage stop working, the hitbox don't spawn in workspace and i don't know how to fix it.
SuperFireSphere Code:
local ts = game:GetService("TweenService")
local replicated = game.ReplicatedStorage
local onepiecepowers = replicated.OnePiecePowers
local Info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local ss = game:GetService("ServerStorage")
local FireWind = ss.FireWind:Clone()
local db = true
onepiecepowers.AceGiantFire.OnServerEvent:Connect(function(Player, Hit)
local char = Player.Character or Player.CharacterAdded:Wait()
local SuperFireSphere = Instance.new("Part")
local hrpp = char.PrimaryPart
local currentcam = workspace.Camera
currentcam.CameraSubject = char.Humanoid
currentcam.CameraType = Enum.CameraType.Scriptable
SuperFireSphere.Shape = Enum.PartType.Ball
SuperFireSphere.Material = Enum.Material.Neon
SuperFireSphere.BrickColor = BrickColor.new("New Yeller")
SuperFireSphere.Anchored = true
SuperFireSphere.CanCollide = false
task.wait(.2)
local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.Velocity = CFrame.lookAt(hrpp.CFrame.Position, Hit.Position).LookVector * 150
hrpp.Position += Vector3.new(0, 50 , 0)
hrpp.Anchored = true
SuperFireSphere.Parent = workspace
FireWind:SetPrimaryPartCFrame(hrpp.CFrame)
FireWind.Parent = workspace
SuperFireSphere.Position = char.PrimaryPart.Position + Vector3.new(0,15,0)
for i = 1, 15 do
task.wait(.1)
SuperFireSphere.Size += Vector3.new(2,2,2)
end
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
local tween = ts:Create(parts, Info, {Transparency = 1})
tween:Play()
end
end
local connect = SuperFireSphere.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= char.Humanoid and db == true then
db = false
hit.Parent.Humanoid.Health -= char.DevilFruitPoints.Value
task.wait(2)
db = true
end
end)
local FireParticle = ss.Fire:Clone()
FireParticle.Parent = SuperFireSphere
SuperFireSphere.Anchored = false
bv.Parent = SuperFireSphere
task.wait(2)
hrpp.Anchored = false
FireWind.Parent = ss
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
parts.Transparency = 0
end
end
task.wait(5)
SuperFireSphere:Destroy()
connect:Disconnect()
end)
Combat:
local replicated = game:GetService("ReplicatedStorage")
local combatevent = replicated.Combat
local servstore = game:GetService("ServerStorage")
local runservice = game:GetService("RunService")
local db = true
combatevent.OnServerEvent:Connect(function(Player)
local char = Player.Character or Player.CharacterAdded:Wait()
local hbox = Instance.new("Part")
hbox.Size = Vector3.new(1.5,1.5,1.5)
hbox.Anchored = true
hbox.CanCollide = false
local connection = runservice.Heartbeat:Connect(function(dt)
hbox.Position = char.RightHand.Position
end)
local tconnection = hbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= char.Humanoid and db then
db = false
hit.Parent.Humanoid.Health -= char.Strenght.Value
task.wait(.1)
db = true
end
end)
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://10497535355"
animation.Parent = char.Humanoid
local loadanim = char.Humanoid:LoadAnimation(animation)
loadanim:Play()
hbox.BrickColor = BrickColor.new("Really red")
hbox.Transparency = 0.5
hbox.Name = Player.Name
print(hbox.Name)
task.wait(.2)
hbox:Destroy()
tconnection:Disconnect()
loadanim:Stop()
connection:Disconnect()
end)
Does someone have any suggestion to help me to fix that??
.........................................................

Unknown Global in Roblox Studio

Basically I am not sure how to define Items on line 4 and 27. If someone can help me that would be great. It just says in the output, unknown global "Items".
for i,v in pairs(Buttons:GetChildren()) do
local NewItem = BoughtItems:FindFirstChild(v.Item.Value)
if NewItem ~= nil then
Items[NewItem.Name] = NewItem:Clone()
NewItem:Destroy()
else
v.Transparency = 1
v.CanCollide = false
end
if v:FindFirstChild("Dependency") then
coroutine.resume(coroutine.create(function()
v.ButtonPart.Transparency = 1
v.ButtonPart.CanCollide = false
if BoughtItems:WaitForChild(v.Dependency.Value, 100000) then
v.ButtonPart.Transparency = 0
v.ButtonPart.CanCollide = true
end
end))
end
v.ButtonPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Values.OwnerValue.Value == Player then
if v.ButtonPart.CanCollide == true and v.ButtonPart.Transparency == 0 then
if Player:WaitForChild("leaderstats").Cash.Value >= v.Price.Value then
Player.leaderstats.Cash.Value -= v.Price.Value
Items[v.Item.Value].Parent = BoughtItems
v:Destroy()
end
end
end
end
end)
end

How to score when killing an NPC

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

Resources