Roblox Functions are running 20+ times - roblox

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)

Related

Descendantadded not firing?

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)

The NPC follows the part but when i go near the NPC they leave the part and start going to random directions

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.

How do I change a value in the leaderstats via touching a part? (Roblox)

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

Why is my flight script not functioning correctly?

I'm making a flight script to use for ProtoSmasher and it's not working as intended. I want it to have a toggle button (G) and then be able to fly with a button (W). Instead of that, to get it to work I have to hold W and then press G; but if I try to let go of w to stop in the air I have to press G again.
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character
local hum = char.Humanoid
local Torso = char.HumanoidRootPart
local Mouse = plr:GetMouse()
local toggle = false
local wToggle = false
Mouse.KeyDown:Connect(function(key)
if key == "w" then
wToggle = true
end
if key == "g" then
if toggle == false then
toggle = true
local BV = Instance.new("BodyVelocity",Torso)
BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
while wToggle == true do
BV.Velocity = Mouse.Hit.lookVector*200
wait()
end
end
if toggle == true then
toggle = false
Torso:FindFirstChildOfClass("BodyVelocity"):remove()
end
end
end)
Mouse.KeyUp:Connect(function(key)
if key == "w" then
wToggle = false
end
end)
It looks like you are trying to connect to keyboard key presses, but you are doing so in the event listener for mouse button presses.
Try something like this :
local isFlying = false
local function onKeyPress(actionName, userInputState, inputObject)
local isKeyDown = userInputState == Enum.UserInputState.Begin
local isKeyUp = userInputState == Enum.UserInputState.End
if actionName == "toggleFlight" then
if isKeyUp then
isFlying = not isFlying
end
elseif actionName == "fly" then
if isKeyDown then
-- start flying forward
elseif isKeyUp then
-- stop flying
end
end
end
-- listen for keyboard input
game.ContextActionService:BindAction("toggleFlight", onKeyPress, false, Enum.KeyCode.G)
game.ContextActionService:BindAction("fly", onKeyPress, false, Enum.KeyCode.W)

ROBLOX GUI/Leaderboard stats

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