Im trying to get data to show from a PlayersGui onto the leaderboard but nothing seems to work, i have asked on the forums but cant seem to get the help i need.
Here is my current script, can someone tell me what is wrong/what i am missing.
(it doesnt show up the leaderboard, just my name)
local Settings = require(script.Parent.Settings)
script.Parent = game.ServerScriptService
--Players = game.Players.LocalPlayer
--pg=Players.Player:WaitForChild("PlayerGui")
--diamonds=pg:WaitForChild("Ores").Frame.diamonds.Number.Bar
stands = {}
CTF_mode = false
function onHumanoidDied(humanoid, player)
local stats = player:findFirstChild("leaderstats")
if stats ~= nil then
local deaths = stats:findFirstChild(Settings.LeaderboardSettings.DeathsName)
if deaths then
deaths.Value = deaths.Value + 1
end
-- do short dance to try and find the killer
if Settings.LeaderboardSettings.KOs then
local killer = getKillerOfHumanoidIfStillInGame(humanoid)
handleKillCount(humanoid, player)
end
end
end
function onPlayerRespawn(property, player)
-- need to connect to new humanoid
if property == "Character" and player.Character ~= nil then
local humanoid = player.Character.Humanoid
local p = player
local h = humanoid
if Settings.LeaderboardSettings.WOs then
humanoid.Died:connect(function() onHumanoidDied(h, p) end )
end
end
end
function getKillerOfHumanoidIfStillInGame(humanoid)
-- returns the player object that killed this humanoid
-- returns nil if the killer is no longer in the game
-- check for kill tag on humanoid - may be more than one - todo: deal with this
local tag = humanoid:findFirstChild("creator")
-- find player with name on tag
if tag ~= nil then
local killer = tag.Value
if killer.Parent ~= nil then -- killer still in game
return killer
end
end
return nil
end
function handleKillCount(humanoid, player)
local killer = getKillerOfHumanoidIfStillInGame(humanoid)
if killer ~= nil then
local stats = killer:findFirstChild("leaderstats")
if stats ~= nil then
local kills = stats:findFirstChild(Settings.LeaderboardSettings.KillsNames)
if kills then
if killer ~= player then
kills.Value = kills.Value + 1
else
kills.Value = kills.Value - 1
end
else
return
end
end
end
end
-----------------------------------------------
function findAllFlagStands(root)
local c = root:children()
for i=1,#c do
if (c[i].className == "Model" or c[i].className == "Part") then
findAllFlagStands(c[i])
end
if (c[i].className == "FlagStand") then
table.insert(stands, c[i])
end
end
end
function hookUpListeners()
for i=1,#stands do
stands[i].FlagCaptured:connect(onCaptureScored)
end
end
function onPlayerEntered(newPlayer)
if CTF_mode == true then
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
local captures = Instance.new("IntValue")
captures.Name = "Captures"
captures.Value = 0
captures.Parent = stats
-- VERY UGLY HACK
-- Will this leak threads?
-- Is the problem even what I think it is (player arrived before character)?
while true do
if newPlayer.Character ~= nil then break end
wait(5)
end
stats.Parent = newPlayer
else
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
local kills = false
if Settings.LeaderboardSettings.KOs then
kills = Instance.new("IntValue")
kills.Name = Settings.LeaderboardSettings.KillsName
kills.Value = 0
end
local deaths = false
if Settings.LeaderboardSettings.WOs then
deaths = Instance.new("IntValue")
deaths.Name = Settings.LeaderboardSettings.DeathsName
deaths.Value = 0
end
local cash = false
if Settings.LeaderboardSettings.ShowCurrency then
cash = Instance.new("StringValue")
cash.Name = Settings.CurrencyName
cash.Value = 0
end
local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(newPlayer.Name)
if PlayerStats ~= nil then
if cash then
local Short = Settings.LeaderboardSettings.ShowShortCurrency
PlayerStats.Changed:connect(function()
if (Short) then
cash.Value = Settings:ConvertShort(PlayerStats.Value)
else
cash.Value = Settings:ConvertComma(PlayerStats.Value)
end
end)
end
end
if kills then
kills.Parent = stats
end
if deaths then
deaths.Parent = stats
end
if cash then
cash.Parent = stats
end
-- VERY UGLY HACK
-- Will this leak threads?
-- Is the problem even what I think it is (player arrived before character)?
while true do
if newPlayer.Character ~= nil then break end
wait(5)
end
local humanoid = newPlayer.Character.Humanoid
humanoid.Died:connect(function() onHumanoidDied(humanoid, newPlayer) end )
-- start to listen for new humanoid
newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )
stats.Parent = newPlayer
end
end
function onCaptureScored(player)
local ls = player:findFirstChild("leaderstats")
if ls == nil then return end
local caps = ls:findFirstChild("Captures")
if caps == nil then return end
caps.Value = caps.Value + 1
end
findAllFlagStands(game.Workspace)
hookUpListeners()
if (#stands > 0) then CTF_mode = true end
game.Players.ChildAdded:connect(onPlayerEntered)
Try putting this inside the function "handleKillCount":
function handleKillCount(humanoid, player)
local findkiller = getKillerOfHumanoidIfStillInGame(humanoid)
local killer = game.Players[findKiller.Value]
if killer ~= nil then
local stats = killer:findFirstChild("leaderstats")
if stats ~= nil then
local kills = stats:findFirstChild(Settings.LeaderboardSettings.KillsNames)
if kills then
if killer ~= player then
kills.Value = kills.Value + 1
else
kills.Value = kills.Value - 1
end
else
return
end
end
end
end
Related
I seem to be having an issue of every time I want to pass a function when an event happens such as a Humanoid.Died() or a BindableEvent it runs the function 20+ times, also adding to an IntegerValue that many times as well. I have tried debounces, BindableEvents, connection:Disconnect(), etc. Is there something I'm doing wrong or missing?
Updated original code:
function module.damageHumanoid(attacker, player, humanoid, damage)
local connection
local debounce = false
connection = humanoid.Died:Connect(function()
if not debounce then
debounce = true
connection:Disconnect()
module.onDeath(player)
end
end)
if player:FindFirstChild("damage") == nil then
newFolder = Instance.new("Folder")
newFolder.Name = "damage"
newFolder.Parent = player
end
if newFolder:FindFirstChild(tostring(attacker)) == nil then
record = Instance.new("IntValue")
record.Name = tostring(attacker)
record.Parent = newFolder
end
if record.Value >= 100 then -- sanity check
else
record.Value = record.Value + damage
end
return humanoid:TakeDamage(damage)
end
Which triggers this code:
function module.onDeath(player)
if player:FindFirstChild("damage") ~= nil then
local folder = player:WaitForChild("damage")
local contents = folder:GetChildren()
for i,v in pairs(contents) do
local item = folder[tostring(v)]
damages[item.Name] = item.Value
end
end
local debounce = false
if not debounce then
debounce = true
module.handleKill(damages)
end
end
This is the sword script's function:
function Blow(Hit)
if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
return
end
local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
if not RightArm then
return
end
local RightGrip = RightArm:FindFirstChild("RightGrip")
if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
return
end
local character = Hit.Parent
if character == Character then
return
end
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid or humanoid.Health == 0 then
return
end
local player = Players:GetPlayerFromCharacter(character)
if player and player == Player then
return
end
UntagHumanoid(humanoid)
TagHumanoid(humanoid, Player)
module.damageHumanoid(plur, player, humanoid, Damage)
end
So I guess the way I was trying to use debounce isn't correct anymore. I referred to the Roblox documentation and found this: https://create.roblox.com/docs/scripting/scripts/debounce
If anyone would like to know what ended up working for me here it is:
humanoid.Died:Connect(function()
if not humanoid:GetAttribute("Killed") then
humanoid:SetAttribute("Killed", true)
module.onDeath(player)
task.wait(10)
humanoid:SetAttribute("Killed", true)
end
end)
Sound = script.Parent.Sound
Image = script.Parent.ScreenGui
Ready = true
function onTouch(t)
local a = t.Parent:FindFirstChild("Humanoid")
if a ~= nil and Ready == true then
Ready = false
local b = Image:Clone()
local player = game.Players:FindFirstChild(a.Parent.Name)
b.Parent = player.PlayerGui
script.Parent.Sound:Play()
wait(2)
b:remove()
wait(1)
Ready = true
end
end
script.Parent.Touched:connect(onTouch())
Error: Workspace.Part.Script:5: attempt to index nil with 'Parent' - Server - Script:5
Ah very simple issue
Sound = script.Parent.Sound
Image = script.Parent.ScreenGui
Ready = true
function onTouch(t)
local a = t.Parent:FindFirstChild("Humanoid")
if a ~= nil and Ready == true then
Ready = false
local b = Image:Clone()
local player = game.Players:FindFirstChild(a.Parent.Name)
b.Parent = player.PlayerGui
script.Parent.Sound:Play()
wait(2)
b:remove()
wait(1)
Ready = true
end
end
script.Parent.Touched:Connect(onTouch)
Omit the braces on the last line, because you were invoking the function onTouch with no parameters, now Connect invokes it with the part parameter, simple fix. https://developer.roblox.com/en-us/api-reference/event/BasePart/Touched
Reply if you need any more help :)
-- Harvey
im trying to make it so that the enemy moves to the sound value parent's position but descendantadded is not firing, i added a little print thingy on the if statement for soundvalue but it doesnt print.
local debounce = false
local prevpath = nil
workspace.DescendantAdded:Connect(function(sound)
--for i,sound in pairs(workspace:GetDescendants()) do
if sound.Name == "Soundvalue" and sound:IsA("StringValue") then
print("Founded")
local hrp = script.Parent:WaitForChild("Torso")
local ischasing = script.Parent:WaitForChild("Ischasing")
if ischasing.Value == false then
if not debounce then
debounce = true
if prevpath ~= nil then
prevpath:Destroy()
end
if sound.Parent and sound.Parent:IsA("BasePart") then
print("aaaaa")
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(hrp.Position, sound.Parent.Position)
local waypoints = path:GetWaypoints()
prevpath = path
for i,waypoint in pairs(waypoints) do
if ischasing.Value == false then
script.Parent.Enemy:MoveTo(waypoint.Position)
script.Parent.Enemy.MoveToFinished:Wait()
end
end
end
debounce = false
end
end
end
--end
end)
So i'm trying to make an npc move to a part when i click on the npc, when i clicked on it all seemed well until i went near the npc, at that point it left the part and started moving to random directions. Is there something wrong?
Script:
local value = script.Parent.FollowSurvivor
local value2 = workspace.Values.MoveToValue
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 1000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(0.5)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
if value.Value == true then
script.Parent.Zombie:MoveTo(target.Position, target)
end
if value.Value == false then
script.Parent.Zombie:MoveTo(workspace.MoveToPart.Position)
end
end
end
This script gets the nearest humanoid and goes towards it. When you walked next to it, it found your humanoid and it started following you.
Here's the error and what I've tried.
Error: Workspace.Part.Script:4: attempt to index nil with 'leaderstats'
My code:
Making Leaderstats:
game.Players.PlayerAdded:Connect(function(plr)
local ls = Instance.new("Model")
ls.Name = "leaderstats"
ls.Parent = plr
local m = Instance.new("IntValue")
m.Name = "Stars"
m.Value = 0
m.Parent = ls
end)
Touch Code:
local collected = false
script.Parent.Touched:Connect(function()
if collected == false then
game.Players.LocalPlayer.leaderstats.Stars.Value = game.Players.LocalPlayer.leaderstats.Stars.Value + 1
end
collected = true
end)
A server script would suit your need better. In a localscript, the change would only appear for the player. Also, it's better practice to use the Players service offered by Roblox. Here's an example:
local Players = game:GetService('Players')
local collected = false
script.Parent.Touched:Connect(function(partTouched)
if partTouched.Parent:IsA('Model') and partTouched.Parent:FindFirstChild('Humanoid') and partTouched:IsA('BasePart') then -- Is this a player? If so, then..
local player = Players:GetPlayerFromCharacter(partTouched.Parent)
if collected == false then
player.leaderstats.Stars.Value += 1 -- shorter syntax
end
collected = true
--script.Parent:Destroy() optional if this part won't be used again.
end
end)
If you're planning to use this for many parts, using a central ModuleScript would save you lots of changing things back and forth: https://developer.roblox.com/en-us/api-reference/class/ModuleScript