Why is my flight script not functioning correctly? - roblox

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)

Related

Roblox Functions are running 20+ times

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)

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)

Boat Spawn (Roblox Studio)

I'm trying(I'm very new) to make a game where you have to attack each other with a boat. There is a button on each island and when you press it, a boat should appear. (this works so far)
But as soon as you want to spawn a new boat it removes everyones boat. I thought it makes sense to use player.id but this still won't work. Does anyone know the solution?
Below you will find the code thats hidden in a part.
Thanks!
CODE:
local cd = script.Parent.ClickDetector
local boat = script.Parent.Parent.Firstboat
local button = script.Parent
local debounce = false
game.Players.PlayerAdded:Connect(function(player)
print(player.UserId)
local plruserid = player.UserId
print(player.Name)
boat.Parent = game.ServerStorage
cd.MouseHoverEnter:Connect(function()
button.Transparency = 0.5
end)
cd.MouseHoverLeave:Connect(function()
button.Transparency = 0
end)
cd.MouseClick:Connect(function()
local SetNameToBoat = plruserid
print (SetNameToBoat)
local oldboat = workspace:FindFirstChild(SetNameToBoat)
if not debounce then
if oldboat then
oldboat : destroy()
end
debounce = true
local NewBoat = boat:clone()
NewBoat.Name = (SetNameToBoat)
NewBoat.Parent = game.Workspace
wait(5)
debounce = false
end
end)
end)
I simplified the code and found the solution:
local cd = workspace.Button:WaitForChild('ClickDetector')
local boat = game:GetService('ServerStorage'):WaitForChild('Boat')
local button = workspace:WaitForChild('Button')
local debounce = false
cd.MouseHoverEnter:Connect(function()
button.Transparency = 0.5
end)
cd.MouseHoverLeave:Connect(function()
button.Transparency = 0
end)
cd.MouseClick:Connect(function(player)
local plruserid = player.UserId
local SetNameToBoat = plruserid
print(SetNameToBoat)
local oldboat = workspace:FindFirstChild(SetNameToBoat)
if not debounce then
if oldboat then
oldboat:destroy()
end
debounce = true
local NewBoat = boat:clone()
NewBoat.Name = SetNameToBoat
NewBoat.Parent = game.Workspace
wait(5)
debounce = false
end
end)

(Also roblox related)Countdown freezes at a random number

so there are 2 scripts: script and localscript.
The script determines how many seconds there will be and then it fires it to all player clients & the localscript changes the text to seconds. But when i tested the game the countdow freezes either at a number or a random number anyway here's the script again:
script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("IntermissonEvent")
local secondsRemaining = 15
for t = secondsRemaining, 0, -1 do
remoteEvent:FireAllClients(t)
wait(1)
end
localscript:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("IntermissonEvent")
local function onTimerUpdate(seconds)
script.Parent.Text =(seconds)
wait(15)
script.Parent.Parent.CountdownText.Visible = false
script.Parent.Parent.IntermissionText.Visible = false
script.Parent.Parent.TextLabel.Text ="Vote for a Map!"
script.Parent.Parent.MapVotingFrame.Visible = true
end
remoteEvent.OnClientEvent:Connect(onTimerUpdate)
Your LocalScript is running all of this logic every single time the server fires the event. Instead of waiting, check the value with an if-then.
local function onTimerUpdate(seconds)
script.Parent.Text = tostring(seconds)
if seconds == 0 then
script.Parent.Parent.CountdownText.Visible = false
script.Parent.Parent.IntermissionText.Visible = false
script.Parent.Parent.TextLabel.Text = "Vote for a Map!"
script.Parent.Parent.MapVotingFrame.Visible = true
end
end

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