Tool ability only being able to be used once? - lua

So, I have a tool that allows the player to dash, yet for some reason the cooldown doesnt seem to work correctly. Does anyone know why the player can't use the ability again?
-- Locals --
local Tool = script.Parent
local Cooldown = false
local mouse=game.Players.LocalPlayer:GetMouse()
local sound=Instance.new("Sound")
-- Main Script --
mouse.KeyDown:Connect(function(key)
if key=="e" then
if Cooldown == false then
Cooldown = true
sound.Parent=Tool.Handle
sound.Volume=3
sound.SoundId="rbxassetid://9079020013"
sound:Play()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 60
wait(.5)
sound:Destroy()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
wait(5)
Cooldown = false
end
end
end)
The goal is for the player to have an E ability that makes them dash, play a sound effect, then stop after half a second. Then, 5 seconds later, the player could do it again. All of this happened, except the player wasnt able to use the ability again.

Have you try this yet, a tool can be activated.
script.Parent.activated:Connect(function()
local sound=Instance.new("Sound")
if Cooldown == false then
Cooldown = true
sound.Parent=Tool.Handle
sound.Volume=3
sound.SoundId="rbxassetid://9079020013"
sound:Play()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 60
wait()
sound:Destroy()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
wait(5)
Cooldown = false
end
end)

Related

Can Someone Help Me Make A Script that When you click a TextButton It runs the script?

I wanted to make a Gui that when you press a TextButton to size your BodyWidth, BodyDepth, Head and BodyHeight and also change your walkspeed nad jumpower. I wanted it to understand when you clicked the button and run the script but I can't do it alone.
Here is the script:
--The error is here
game.Players.PlayerAdded:Connect(function(Player)
--Maybe here too
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character.Humanoid
Humanoid.BodyDepthScale.Value = 10
Humanoid.BodyHeightScale.Value = 10
Humanoid.BodyWidthScale.Value = 10
Humanoid.HeadScale.Value = 10.5
Humanoid.WalkSpeed = 70
Humanoid.JumpPower = 0
end)
end)
end)
I tried to make an if condition But I still couldn't do the script.
First add a LocalScript to the button, and type:
local LocalPlayer = game:GetService("Players").LocalPlayer
local char = LocalPlayer.Character
local Humanoid = char:WaitForChild("Humanoid")
function Click ()
Humanoid.WalkSpeed = 70
Humanoid.JumpPower = 0
-- scale the meshes right here like char.Head.Size = Vector3.new(3,3,3)
end
script.Parent.MouseButton1Up:Connect(Click)
You can scale the meshes not the Humanoid Values

When ever I run this music script it works except for changing the GUI text to the current song name

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mutebutton = script.Parent
local track = game.StarterGui.MusicPlayer.Track
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local c = math.random(1, #b)
local d = b[c]
while true do
wait()
d:Play()
local connection = mutebutton.MouseButton1Click:Connect(function()
if d.IsPaused then
d:Resume()
mutebutton.Text = "Pause"
else
d:Pause()
mutebutton.Text = "Resume"
end
end)
d.Ended:Wait()
connection:Disconnect()
d:GetPropertyChangedSignal("SoundId"):Connect(function()
track.Text = d.Name
end)
end
Everything works until the Get Property Changed Signal event. Whenever I load into the game the box that should have the song name will instead not do anything and keep it as an empty box. Anyone know why?
It seems you have a single mute button (not one for each song), so why are you putting the pausing functionality inside a while loop?
You're not changing the sound id anywhere, is there more code that you're not showing?
You have a variable "local d" but this is only ever calculated outside of the loop so it will just repeat the same song over.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mutebutton = script.Parent
local track = game.StarterGui.MusicPlayer.Track
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local c = math.random(1, #b)
local song -- definite it originally here as nil so we can access it outside of the loop
mutebutton.MouseButton1Click:Connect(function()
if song.IsPaused then
song:Resume()
mutebutton.Text = "Pause"
else
song:Pause()
mutebutton.Text = "Resume"
end
end)
while true do
song = b[math.random(1, #b)]
track.Text = song.Name
song:Play()
song.Ended:Wait()
task.wait()
end
This might work, but I can't see your Explorer

Tried making coin counter, nothing happens when tested

So, I have a game on Roblox currently in development, and I am trying to make a coin counter. Basically, when the coin is collected, a NumberValue in StarterPlayer changes by +1. Then, A TextLabel will loop to check the value of the NumberValue. It will then change the text to display the value of the NumberValue.
However, when I playtest, nothing happens. The counter doesn't change, but the NumberValue does.
Here's the code for the Coin:
local coin = script.Parent
local sp = game:GetService("StarterPlayer")
local count = sp.Coins
local p = game:GetService("Players")
local Touched = false
coin.Touched:Connect(function()
count.Value += 1
local Sparkles = Instance.new("Sparkles")
Sparkles.Parent = coin
Sparkles.SparkleColor = Color3.fromRGB(255, 255, 0)
wait(2)
count.Value += 1
coin:Destroy()
end)
And the code for the TextLabel:
local StarterPlayer = game:GetService("StarterPlayer")
local Coins = StarterPlayer.Coins
local CoinLabel = script.Parent
while true do
CoinLabel.Text = Coins.Value
end
Does anyone know how to fix this?
The reason why your TextLabel is never updating is because you have an infinite loop that causes the script to timeout and stop running.
while true do
CoinLabel.Text = Coins.Value
end
The simple fix for that is to only update the TextLabel when the NumberValue changes :
Coins.Changed:Connect(function()
CoinLabel.Text = Coins.Value
end)
But something else is a little weird too. Objects in StarterPlayer are expected to be copied to each player when they join, but StarterPlayer.Coins is not, and as it is right now, it is acting as a global counter for everyone. So your code is modifying the Coins object that would be copied over to new players, not the one that each player has.
But according to the documentation for StarterPlayer, only a few kinds of objects in StarterPlayer are copied to players.
A StarterPlayerScripts instance, with scripts that run once for each player.
A StarterCharacterScripts instance, with scripts to add to each player’s character every time they spawn.
A Humanoid instance named StarterHumanoid, which will be used as the default humanoid for each player’s character.
A Model instance named StarterCharacter, which will be used as the character model for all players
So the NumberValue wouldn't have been copied to each player even if this logic was set up to locate it properly! So, we need to make a few changes :
A Coins NumberValue needs to be created for each player when they join the game.
You need to update the paths to the individual Coins NumberValues for each player.
Your TextLabel code should listen for the
NumberValue.Changed
signal, that way your TextLabel will only update when the
NumberValue does.
So in a Script somewhere like ServerScriptService, add this to create a Coins NumberValue for each Player :
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
-- give each player a Coins object
local coins = Instance.new("NumberValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = player
end)
Next, update the paths in each script to reflect the new location of the object. In the first script, use the player that touched the coin to locate the Player object :
local coin = script.Parent
local Players = game:GetService("Players")
local Touched = false
coin.Touched:Connect(function(otherPart)
-- make sure a player touched this
if otherPart.Parent:FindFirstChild("Humanoid") == nil then
return
end
-- only call this function once
if Touched then
return
end
Touched = true
-- get the player
local player = Players:GetPlayerFromCharacter(otherPart.Parent)
if not player then
return
end
-- increment the player's coin counter
player.Coins.Value += 1
-- show some sparkles
local Sparkles = Instance.new("Sparkles")
Sparkles.SparkleColor = Color3.fromRGB(255, 255, 0)
Sparkles.Parent = coin
-- destroy the coin after a moment
wait(2)
coin:Destroy()
end)
Finally, update the LocalScript that updates the TextLabel so that it properly finds the NumberValue and updates only when the NumberValue changes :
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Coins = player.Coins
local CoinLabel = script.Parent
Coins.Changed:Connect(function()
CoinLabel.Text = Coins.Value
end)

Roblox script running multiple times?

I need a bit of help. Basically I have this code:
local plyIsEntered = false
function onTouched(hit)
plyIsEntered = true
if not plyIsEntered then
end
if plyIsEntered then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local ply = humanoid.Parent
if humanoid ~= nil then
print("Hit")
local playerName = hit.Parent.Name
print(playerName)
local laserEmitter = game.Workspace["Enterance PC"]:FindFirstChild("laserEmitter")
local scanLaser = Instance.new("Part", game.Workspace)
scanLaser.Position = laserEmitter.Position
scanLaser.Name = "ScanLaser"
scanLaser.Size = Vector3.new(1,1,1)
local scanLaserMesh = Instance.new("SpecialMesh", game.Workspace.ScanLaser)
scanLaserMesh.Name = "Cone mesh"
scanLaserMesh.MeshType = ""
plyIsEntered = false
end
end
end
script.Parent.Touched:connect(onTouched)
Now I'm checking if the player touches a box, it has no collisions and is invisible; when they do I want to create a laser that will scan them and open a door. The problem I'm having is when I walk into the trigger box it creates 8 or 9 blocks. One of those blocks is the block I'm applying a mesh too.
What I need to do is make sure it's only running once and not creating more than 1 brick. Hopefully someone can help me!
I believe to fix this you'll need to add a debounce.
You see, the touched event of a Part actually fires many times, so your code will execute multiple times if it is inside of the event.
To fix this, we use a debounce, which means your code won't execute if your part is touched too much in the same time frame. Here is an example:
local debounce = false
part.Touched:connect(function()
if debounce == false then
debounce = true
--Your code goes here.
wait(1)--Wait one second until you'll be able to execute the code again.
debounce = false
end
end)
To read more on debounces: http://wiki.roblox.com/index.php?title=Debounce

My script in Roblox works fine, but once I added a debounce, it still worked perfectly, but only sometimes?

For example: The script works fine in one game session, but then in another, it doesn't work at all; almost as if there's some sort of random chance for the script to be deleted or completely ignored. If I remove the debounce, there's a 100% chance for the script to work once again. What could possibly be going wrong here?
local radius = script.Parent
local light = radius.Parent.Light
local sound = radius.Parent.lighton
local debounce = false
radius.Touched:connect(function(hit)
if debounce == false then debounce = true
if game.Players:GetPlayerFromCharacter(hit.Parent) then
light.PointLight.Brightness = 10
light.Material = "Neon"
sound:Play()
wait(5.5)
light.PointLight.Brightness = 0
light.Material = "Plastic"
sound:Play()
wait(0.5)
debounce = false
end
end
end)
Your problem is one of scoping. The debounce will always be set to true, but will only sometimes be set back to false. If it doesn't get changed, the function will obviously never be run again. You'll want to avoid lines like if debounce == false then debounce = true, as they make it more difficult for you to notice that the debounce is not changed in the same scope.
Fixed code:
local radius = script.Parent
local light = radius.Parent.Light
local sound = radius.Parent.lighton
local debounce = false
radius.Touched:connect(function(hit)
if debounce == false then
debounce = true
if game.Players:GetPlayerFromCharacter(hit.Parent) then
light.PointLight.Brightness = 10
light.Material = "Neon"
sound:Play()
wait(5.5)
light.PointLight.Brightness = 0
light.Material = "Plastic"
sound:Play()
wait(0.5)
end
debounce = false
end
end)
Note that both statements changing the value of debounce align.

Resources