So when I touch a button, i’ve made it so it prints the plr and for 1 second i touch it, it says nil and then it says the players name after that… why is it saying nil for a short period?
local TycoonObjects = game.ReplicatedStorage.Tycoon1:FindFirstChild("Tycoon1Objects")
local Owner = script.Parent.Parent.Owner
local Players = game:GetService("Players")
local TycoonNumber = game.ReplicatedStorage:FindFirstChild(script.Parent.Parent.TycoonNumber.Value)
--[[
- Loop through all the buttons and find the button that was touched.
- Get the touched buttons price, if the player has the right amount,
proceed with purhcase, if not notify the user they don't have the right amount.
- If the player has bought the button successfully,
were going to search for the object under the players tycoon buttons in
replicated storage, and find the object that the button
was linked to.
were then going to make the right object from rs and move it to objects model
in the players tycoon.
]]--
for i, button in pairs(ButtonsGroup:GetDescendants()) do
if button.Name == "Head" then
button.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
print(plr)
local leaderstats = plr.leaderstats
local buttonprice = button:FindFirstChild("Price")
local buttonobject = button:FindFirstChild("ObjectValue")
print(buttonobject.Value)
if leaderstats.Gems.Value >= buttonprice.Value then
print("You have enough money. You have bought ".. buttonobject.Value)
TycoonNumber.Objects:FindFirstChild(buttonobject.Value).Parent = ButtonsGroup
elseif leaderstats.Gems.Value < buttonprice.Value then
print("You have not got enough money for this. ")
end
end)
end
end ```
other parts can trigger touched events too so you should if its a player/character first
for i, button in pairs(ButtonsGroup:GetDescendants()) do
if button.Name == "Head" then
button.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildWhichIsA("Humanoid")
Yes that worked, thanks also quick question, how would I make it so the output doesn’t get flooded when I press on this button. I mean, they’re only going to press it once because it will disappear. But, it’s annoying going into game stepping and my output being flooded. I could just remove the print code, but then I can’t debug… was going to make a debounce maybe? but im not sure which part I could set it up in my script.
debounce = false
for i, button in pairs(ButtonsGroup:GetDescendants()) do
if button.Name == "Head" and debounce = false then
button.Touched:Connect(function(hit)
debounce = true
is my code messy btw, i’ve only been coding for a few months now still pretty new, been off and on it recently… just learning new things and testing things out. Really liking it to be fair it’s fun and it tests ur brain.
Also, I’ve heard that using wait is bad? is this true and why so?
I don’t really watch tutorials I mainly like to use my reference sources as the devwiki and here.
it’s good enough
and a tip for setting alot of variables:
instead of doing:
local buttonprice = button:FindFirstChild(Price)
local buttonobject = button:FindFirstChild(ObjectValue)
you can minimize the lines by doing:
local buttonprice, buttonobject = button:FindFirstChild(Price), button:FindFirstChild(ObjectValue)
and using wait() can be bad because itll pause/yield the code, so an alternative would be using coroutines. you can use wait() but it really depends on ur code, mostly coroutines are used in modules, global use scripts and specific local stuff