I was working on the script which detects a player, when his tool touched something, and he receives some XP, but I got one problem.
script.Parent.Tool.Part.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local expPoints = player:findFirstChild("leaderstats")
if expPoints ~= nil then
local points = expPoints:findFirstChild("Exp")
if points ~= nil then
points.Value = points.Value + 10
end
end
end)
When I test out the game I receive this in the output:
Workspace.AwesomelyDeveloper.XP_Script:3: attempt to index local âplayerâ (a nil value)
Script is in the StarterPlayer>StarterCharacterScripts>XP_Script and here is the place of the tool:
local PlayersService = game:GetService("Players")
local function onTouch(hit)
local player = PlayersService:GetPlayerFromCharacter(hit.Parent)
local expPoints = player:findFirstChild("leaderstats")
if expPoints ~= nil then
local points = expPoints:findFirstChild("Exp")
if points ~= nil then
points.Value = points.Value + 10
end
end
end
script.Parent.Touched:connect(onTouch)
I tried using this script inside of the part in the workspace, so once I touch that part I receive some XP and it works pretty well, while the XP_Script inside of StarterCharacterScripts tells me that local âplayerâ is a nil value⌠Can someone fix this issue? Thanks.
I think the problem is that your code is working under the assumption that everything it touches must be a character of a Player.
Add a check if player exists before doing anything with that, because GetPlayerFromCharacter returns nil if the given instance does not belong to any playerâs Character.
local function onTouch(hit)
local player = PlayersService:GetPlayerFromCharacter(hit.Parent)
if player then
local expPoints = player:findFirstChild("leaderstats")
if expPoints then
local points = expPoints:findFirstChild("Exp")
if points then
points.Value = points.Value + 10
end
end
end
local PlayersService = game:GetService("Players")
local function onTouch(hit)
local player = PlayersService:GetPlayerFromCharacter(hit.Parent)
if player then
local expPoints = player:findFirstChild("leaderstats")
if expPoints then
local points = expPoints:findFirstChild("Exp")
if points then
points.Value = points.Value + 10
end
end
end
end
script.Parent.Tool.Part.Touched:Connect(onTouch)
It doesnât give an error anymore, but I am not receiving any XP.
I attempted the code you posted above to find any more errors and only changed the line script.Parent.Tool.Part.Touched:Connect(onTouch) to script.Parent.Touched so I could test with a brick and it worked fine with the leaderboard I set up though you might want to add a debounce
I tried using print() method and looks like it prints in the output if print is here:
local PlayersService = game:GetService("Players")
local function onTouch(hit)
local player = PlayersService:GetPlayerFromCharacter(hit.Parent)
print("Touched!")
if player then
local expPoints = player:findFirstChild("leaderstats")
if expPoints then
local points = expPoints:findFirstChild("Exp")
if points then
points.Value = points.Value + 10
end
end
end
end
script.Parent:FindFirstChild("Tool"):WaitForChild("Part").Touched:Connect(onTouch)
If I place print() inside of other ifâs it wonât print anything in the output.
local function OnTouch(Hit)
local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
local LeaderstatsFolder = Player:WaitForChild("leaderstats")
local ExpPoints = LeaderstatsFolder:WaitForChild("Exp")
ExpPoints.Value = ExpPoints.Value + 10
end
end
local function onTouch(hit)
local humanoid = hit.Parent:findFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
print(player)
if humanoid~=nil and player ~= nil then
print("step1")
print("Touched!")
local expPoints = player:findFirstChild("leaderstats")
print("step2")
if expPoints then
print("step3")
local points = expPoints:findFirstChild("Exp")
print("step4")
if points then
print("step5")
points.Value = points.Value + 10
print("step6")
end
end
end
end
end
script.Parent.Touched:Connect(onTouch)
Test this? The prints were just to ensure that every step was running through you can delete them
local debounce = false
local cooldown = 5
local function onTouch(hit)
if debounce == true then return end
debounce = true
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
if not Humanoid then -- if it touches accessories
Humanoid = hit.Parent.Parent:FindFirstChild("Humanoid")
if not Humanoid then
debounce = false
return
end
end
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Leaderstats = Player:WaitForChild("leaderstats") -- fill in with the name of the leaderstats
local ExpPoints = Leaderstats:WaitForChild("Exp") -- fill in the name of the what I think is IntValue
ExpPoints.Value = ExpPoints.Value + 10
wait(cooldown)
debounce = false
end
script.Parent.Touched:Connect(onTouch) -- script should be parented to the part of the tool
I suspect debounce is required. Also check if itâs a LocalScript.
â Updated twice: Fixed the debounce issue after the function uses return.