attempt to index nil with leaderstats - lua

I'm making a game where you ram cars into houses and it unanchored the parts that are touched by a part in the front of the car. I want to have it so that whenever you unanchor a part, you get a coin
local HitPart = script.Parent
local function onTouch(otherPart)
local player = otherPart.Parent
if otherPart then
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
if otherPart then
game.Players.LocalPlayer.leaderstats.coins.Value = game.Players.LocalPlayer.leaderstats.coins.Value + 1
end
end
end
HitPart.Touched:Connect(onTouch)
HitPart is the part that is touching the other parts. However, I keep getting "attempt to index nil with 'leaderstats'" error. does anyone know whats wrong?

Have you created the leaderstats inside the player? Here's an in-depth article about In-game leaderboards by Roblox

The error is telling you that you are trying to access the leaderstats of a player that doesn't exist.
You have a few issues.
Since this is (probably) a Script, game.Players.LocalPlayer doesn't exist. LocalPlayer only exists in LocalScripts.
Since the Touched signal will fire for any object that touches it, you need to add safety checks that the player object actually exists
To make finding a player easier, I would recommend the Players:GetPlayerFromCharacter function. You pass it a Model from the Workspace, and it tells you if you've got a Player.
local HitPart = script.Parent
local function onTouch(otherPart)
local characterModel = otherPart.Parent
local player = game.Players:FindPlayerFromCharacter(characterModel)
if player then
local coins = player.leaderstats.coins
coins.Value = coins.Value + 1
end
end
HitPart.Touched:Connect(onTouch)

Related

The door data is not saved in Roblox

I wrote a door save system.
That is, if the user previously bought them, then when re-entering the game, they must be open.
My code works, but the door doesn't save at all.
-- DoorsDataStore
-- Save Stats Doors
local opend = false
local datastorage = game:GetService("DataStoreService")
local isitopen_1 = datastorage:GetDataStore("Door")
game.Players.PlayerAdded:Connect(function(player)
local boolValueDoors = Instance.new("Folder")
boolValueDoors.Name = "BoolValueDoors"
boolValueDoors.Parent = player
local door_1 = Instance.new("BoolValue")
door_1.Parent = boolValueDoors
door_1.Name = "BoolValueDoor_1"
door_1.Value = isitopen_1:GetAsync(player.UserId)
print("True or False")
print(player.BoolValueDoor_1.Value)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, erromsg = pcall(function()
isitopen_1:SetAsync(player.UserId, player.BoolValueDoor_1.Value)
end)
if erromsg then
warn("Error")
end
end)
-- TouchDoor
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if (humanoid ~= nil) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player.leaderstats.Coins.Value >= script.Parent.Price.Value then
player.leaderstats.Coins.Value -= script.Parent.Price.Value
player.leaderstats.Level.Value += 1
script.Parent:Destroy()
player.BoolValueDoors.BoolValueDoor_1.Value = true
print("Save Door")
end
end
end)
I tried writing this code in different ways, in different versions, tried through validation. My code still doesn't do what I want.
There are multiple possible problems I see:
1.
SetAsync takes a string as the first argument, you are giving it a number value. To fix this use tostring(player.UserId)
2.
When the player first joins, the value will be set to nil, because there is no data in the datastore under the UserId, and nil is not a boolean.
I’m not sure if these are actual issues and I currently cannot check if these are real problems because I’m not on my computer but they may be something you want to change.
Also it would be nice to know if you encountered any errors when executing the code.
You should also make trigger a remote event that opens the door on the clientside in the PlayerAdded function if the value is true.

Attempt to call an instance value

I'm making a game where you ram cars into houses that are made if parts, and when a part at the front of the car hits a otherPart the otherPart becomes unanchored. I'm trying to make a script where when the otherPart becomes unanchored, the player gets + 1 coins, but I keep getting the error above on line 9, any ideas?
code and error
local HitPart = script.Parent
local function onPartTouched(otherPart)
local Player = otherPart.Parent
otherPart.Anchored = false
Player(otherPart.Parent).leaderstats.coins.Value = Player(otherPart.Parent).leaderstats.coins.Value + 1
end
HitPart.Touched:Connect(onPartTouched)
Player is an Instance. You cannot call an instance. You can only function values or values with a __call metamethod.
In Player(otherPart.Parent) you're attempting to call Player which is not allowed.
To get rid of that error remove the two call attempts from your code. You probably wanted to index something else with leaderstats

Roblox Lua Give LocalPlayer weapon doesn't work,

My code: https://justpaste.it/87evk
local original = game:GetService("ReplicatedStorage"):FindFirstChild("CloneSmoke")
local tool = script.Parent
local copy = original:Clone()
tool.Activated:Connect(function()
print("Running...")
local sound = script.Parent.Sound
copy.Parent = script.Parent.Handle
sound:Play()
wait(3)
tool:Destroy()
local plr = game.Players.LocalPlayer
local weapon = game.ReplicatedStorage.Jutsus.Momoshiki["Momoshikis Sword"]
local w2 = weapon:Clone()
w2.Parent = plr.Backpack
end)
Idk what i have to do here to get the Player and give him a Weapon.I tried much but i dont get the right Soloution.
It were nice wenn you help me
First off, make sure this is in a LocalScript
Also change local original = game:GetService("ReplicatedStorage"):FindFirstChild("CloneSmoke") to local original = game:GetService("ReplicatedStorage"):WaitForChild("CloneSmoke") This is because the script loads faster than items in the game, so chances are, the sword hasn't loaded into the game by the time the script runs; the script won't work if it doesn't find the object you are trying to reference.

I am trying to change the Mesh texture of a tool in a players backpack

I made a part and added a script with a function that detects if the part was touched. If a player touches this part I need to access that players Backpack and a find a tool. In this case, it is a Pistol. I then try to change the TextureId of the Pistols mesh, which is inside of the Handle part within the Pistol
I have tried accessing the players backpack by using hit.Parent.Backpack,
but when I touch the part I get an error in the console saying
Backpack is not a valid member of Accessory
Here is the entire script...
function onTouched(hit)
local player = hit.Parent
local p = player.Backpack:FindFirstChild("Pistol")
local h = p:FindFirstChild("Handle")
local m = h:FindFirstChild("Mesh")
local id = m.TextureId
id = "rbxassetid://3707943717"
end
script.Parent.Touched:Connect(onTouched)
The expected result should be:
When a player touches this part it should look for a "Pistol" in the players Backpack and then look for the pistols "Mesh" and change the meshes "TextureId" to whatever texture I have set it to in the script.
I was able to fix this after some more research. My first mistake is that hit.Parent seems to only be the model. So i used that to find the "Humanoid" then from there got the name of the player so that i can find the player in the player list and access the backpack to change the pistols "Mesh.TextureId"
Here is the new script...
function onTouched(m)
local p = m.Parent:FindFirstChild("Humanoid")
if p ~= nil then
local n = p.Parent.Name
local player = game.Players:FindFirstChild(n, false) -- find the player that hit the Part.
local gun = player.Backpack:FindFirstChild("Pistol")
if player == nil then return end -- escape if something goes wrong.
local handle = gun:FindFirstChild("Handle")
local mesh = handle.Mesh
mesh.TextureId = "rbxassetid://3707943717" -- change the gun texture
end
end
script.Parent.Touched:Connect(onTouched)

Roblox: Make an NPC immovable?

I'm currently working on a game on ROBLOX that contains a TON of NPC's. I need a way to make the player not be able to move them around in any way. I've tried anchoring the HumanoidRootPart, and that worked, but it made the NPC unable to move.
Can anyone help?
You could weld the NPC to the ground, if possible.
This could work:
local weld = Instance.new("WeldConstraint")
weld.Part0 = part
weld.Part1 = otherPart
weld.Parent = part
There is more info here on welding.
If you don't need players to clip through said NPCs, you could "unclip" the NPC, allowing players to move through it but not move it.
local function setDescendantCollisionGroup(Descendant)
if Descendant:IsA("BasePart") and Descendant.CanCollide then
-- set collision group
end
end
model.DescendantAdded:Connect(setDescendantCollisionGroup)
for _, descendant in pairs(model:GetDescendants()) do
setDescendantCollisionGroup(descendant)
end
This should be able to be done using a property . Create a startercharacter so players wear this character,and then modify it's customphysicalproperties "friction" , "density" to a very low number.
You should also be able to put such in a script such as when a player join,the children with class "Part" have their density and friction low.
Try something like this:
local NPC = workspace.NPC --The NPC variable
NPC.HumanoidRootPart.Mass = 69420
It should make the NPC alot heavier!
And when you want it to move:
local NPC = workspace.NPC --The NPC variable
NPC.HumanoidRootPart.Mass = 10
That will make the NPC lighter!
So this is the final script:
local NPC = workspace.NPC --The NPC variable
local humanoid = NPC:WaitForChild('Humanoid') --NPC's humanoid
local hrp = NPC.HumanoidRootPart --NPC's HumanoidRootPart
local mass1 = 69420
local mass2 = 10
humanoid.Running:Connect(function(speed)
if speed > 0.001 then
hrp.Mass = mass2
else
hrp.Mass = mass1
end
end)
Make sure to write that code in a ServerScript!
And put the Script inside the NPC if you want.

Resources