Tried making coin counter, nothing happens when tested - lua

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)

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

Having issues with Region3 in roblox lua

I have been trying to work on a Region3 script were when you are in it, it plays a song. But I've come across 2 issues, 1 being it thinks the player is always in it when you the player isn't. And the second being the script runs so fast that it keeps repeating before anything can happen
local RegionPart = game.Workspace.RegionArea
local pos1 = RegionPart.Position - (RegionPart.Size / 2)
local pos2 = RegionPart.Position + (RegionPart.Size / 2)
local Region = Region3.new(pos1, pos2)
while true do
wait()
local burhj = workspace:FindPartsInRegion3(Region, nil, 1000)
local song = game.Workspace.bb
song:Play()
print("THE SCRIPT WORKS!")
end
You query the objects which are in Region, but never used the result and just continued. Loop over burhj and check for valid parts.
In this forum the use of FindFirstChild is suggested:
for i,v in ipairs(burhj) do
local player = v.Parent:FindFirstChild("Humanoid")
if player then
print("player is in region: " + player.Parent.Name)
end
end
Alternatively, you can directly use the player position, if the player object or position is known:
local pos = yourPlayer.HumanoidRootPart.Position
local center = region.CFrame
local size = region.Size
if pos.X > center.X - size.X / 2 and pos.X < center.X + size.X / 2 and ... then
print("player is in region")
end
A helper function, if not already present, might be helpful.
For the second problem, set a flag if the player is in the region. Play the sound when the flag was not already set. Unset the flag if you leave the region.
--here are your vars
local enteredFlag = false
while true do
wait()
if playerIsWithinRegion then --here come whatever approach you chose earlier
if not enteredFlag then
enteredFlag = true
local song = game.Workspace.bb
song:Play()
print("THE SCRIPT WORKS!")
end
else
--no player is in the region, lets reset the flag
enteredFlag = false
end
end

Punch animation from Roblox tutorial is throwing errors

I'm trying to get a punch animation to work from a tutorial, but it never works when I press the designated Keybinding.
I have tried changing values from the tutorial from 1 to the animation length, past the animation length, and even random numbers. I also tried changing some wording to the script.
math.randomseed(tick())
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"
local animation = (03910055905)
local function onPunchFired(plr)
local char = game.Workspace:FindFirstChild(plr.Name)
local humanoid = char.Humanoid
local animation = Instance.new("Animation")
animation.AnimationId = "http://roblox.com/asset/?id="..animations[math.random(1, #animations)]
local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()
end
punchEvent.OnServerEvent:Connect(onPunchFired)
I expect the script to run smooth and punch, but it shows this:
20:46:29.496 - ServerScriptService.ExtremePunch:13: attempt to get length of global 'animations' (a nil value)
This is all it says in the output for errors. I've double checked the replication, but it keeps failing. How can I fix it?
Your error is pointing at this line :
animation.AnimationId = "http://roblox.com/asset/?id="..animations[math.random(1, #animations)]
This is because in the scope of this script, there is no local variable or array named animations.
To fix your code, you just have to fix the number in the animation.AnimationId url. One problem you might have run into was that you had two variables named animation so it failed to construct the url properly for you.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"
local function onPunchFired(plr)
local char = game.Workspace:FindFirstChild(plr.Name)
local humanoid = char.Humanoid
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://3910055905"
local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()
end
punchEvent.OnServerEvent:Connect(onPunchFired)
If you're unsure if your asset id is correct, you can also check by putting it into the url for the Roblox Catalog : https://www.roblox.com/catalog/YOUR_ASSET_ID_NUMBER. If it redirects to your animation, you know you've got the right one.
The animations list does not exist in the code that you showed us. So, add your other punch animations to the animations list that I added and you should be good to go. I didn't test it so if it's not working, HMU.
local replicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent")
punchEvent.Name = "PunchEvent"
punchEvent.Parent = replicatedStorage
local animations = [03910055905, ...]
local function onPunchFired(plr)
local char = game.Workspace:FindFirstChild(plr.Name)
local humanoid = char.Humanoid
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. tostring(animations[math.random(1, #animations)])
local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()
end
punchEvent.OnServerEvent:Connect(onPunchFired)

ROBLOX Get name of a player that clicked a brick

I have this script in a brick:
local giver = 1
function onClicked()
game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value = game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value + giver
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
Now I need to somehow get the player's name that clicked it and put it where I need to.
The ClickDetectors's MouseClick event have the "Clicking Player" as parameter, so you can do it like this:
local giver = 1
function onClicked(Player)
Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + giver
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
However, this requires the FilteringEnabled to be set to false (not recomended).
To solve this, make a LocalScript in the brick with the code:
script.Parent.ClickDetector.MouseClick:connect(function(Player)
game.ReplicatedStorage:WaitForChild("BrickClick"):InvokeServer(script.Parent)
end)
And in a Script placed in the ServerScriptService put:
local Listener = game.ReplicatedStorage:FindFirstChild("BrickClick")
if Listener == nil then
Listener = Instance.new("RemoteFunction")
Listener.Name = "BrickClick"
Listener.Parent = game.ReplicatedStorage
end
function Listener.OnServerInvoke(Player,Brick)
Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + 1
end
I won't point you to the wiki page for further reading, even thought it contains a bit of what you need, it contains too little information.
The ClickDetector's MouseClick info, the guide about FilteringEnabled and the guide about RemoteFunctions are better.
Try this!
script.Parent.MouseClick:Connect(function(Player)
-- Kill The Player
-- The parameter is referring to game.Players So if you want to do a kill button use .Character
Player.Character:BreakJoints()
-- Change The Color To Red (Other details)
script.Parent.Parent.BrickColor = BrickColor.new("Really red")
script.Parent.MaxActivationDistance = 0
-- Wait 4 Secs
wait(5)
-- Change The Color To Green
script.Parent.Parent.BrickColor = BrickColor.new("Lime green")
script.Parent.MaxActivationDistance = 50
end)

Resources