Hey there! I would say I am fairly new to coding properly on Roblox, and I ran into an issue that I cannot solve. I am making a tycoon game, and got the plot handling done (in a different script), but I cannot make a handler for one floor (yes I am making individual floor handlers) Here is the code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local plot = script.Parent
local purchasedItems = plot:WaitForChild("PurchasedItems")
local claimedValue = plot:WaitForChild("Claimed")
local tycoons = ReplicatedStorage:WaitForChild("Tycoons")
local solarTycoon = tycoons:WaitForChild("Solar_Tycoon")
local upgradeButtons = solarTycoon:WaitForChild("Upgrade_Buttons")
local floor1Folder = upgradeButtons:WaitForChild("Floor1")
local function loadFloor1()
for _, child in ipairs(purchasedItems:GetChildren()) do
child:Destroy()
end
for _, button in ipairs(floor1Folder:GetChildren()) do
local clone = button:Clone()
clone.Parent = purchasedItems
clone.CFrame = button.CFrame
print("Cloned button:", clone.Name, "into PurchasedItems")
end
end
claimedValue.Changed:Connect(function()
local owner = claimedValue.Value
if owner ~= "None" and owner ~= "" then
print("Plot claimed by:", owner)
loadFloor1()
end
end)
if claimedValue.Value ~= "None" and claimedValue.Value ~= "" then
loadFloor1()
end
Any help is appreciated, thanks! (I know I should use module scripts and stuff but idk how, pls do not roast me)
1 Like
By the way, it is not the actual code, just testing to see if buttons would spawn after claiming
I don’t see any immediate problems with the logic you used, what’s the intended behaviour and what is the behaviour now?
(as a side note, it might be easier for you to use a table storing floor names, and then using a function like LoadFloor(floorName), to make the code a bit more modular, if you haven’t already)
1 Like
The logic in theory should work, but it doesn’t. The intended behavior is to copy the models from RS to the plots folder in the workspace, but there are no errors, and the code just for some reason doesn’t work.
Also, the Claimed value is already initialized in studio as “None”, as leaving it blank spawns the buttons without the need of claiming the plot. Also, thanks for the feedback on the modular system.
In that case I’d say your best bet is to step through the code using the debugger, so that you can see where the cloned models are actually going. The only thing I can think of is that if the button is a model, you would need to use PivotTo() instead of assigned the CFrame directly
I put the following prints throughout the code:
print("Button Handler Loaded")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local plot = script.Parent
local purchasedItems = plot:WaitForChild("PurchasedItems")
local claimedValue = plot:WaitForChild("Claimed")
local tycoons = ReplicatedStorage:WaitForChild("Tycoons")
local solarTycoon = tycoons:WaitForChild("Solar_Tycoon")
local upgradeButtons = solarTycoon:WaitForChild("Upgrade_Buttons")
local floor1Folder = upgradeButtons:WaitForChild("Floor1")
print("Variables Found")
print("Reading Function: loadFloor1")
local function loadFloor1()
for _, child in ipairs(purchasedItems:GetChildren()) do
print("Destroying children of ", purchasedItems)
child:Destroy()
print("Children of ", purchasedItems, "destroyed successfully")
end
print("Reading buttons for loop")
for _, button in ipairs(floor1Folder:GetChildren()) do
local clone = button:Clone()
clone.Parent = purchasedItems
clone.CFrame = button.CFrame
print("Cloned button:", clone.Name, "into PurchasedItems")
end
end
print("Checking for changes in: ", claimedValue)
claimedValue.Changed:Connect(function()
local owner = claimedValue.Value
if owner ~= "None" and owner ~= "" then
print("Plot claimed by:", owner)
loadFloor1()
end
end)
print("Seeing if the code will actually run")
if claimedValue.Value ~= "None" and claimedValue.Value ~= "" then
print("Loading floor 1")
loadFloor1()
print("Loaded floor 1 successfully")
end
Not all of them were printed. The only ones printed were:
Button Handler Loaded
Variables Found
Reading Function: loadFloor1
Checking for changes in: Claimed
Seeing if the code will actually run
bumping this I still need help
print("Plot claimed by:", owner) and print("Loading floor 1") never print, which means there is something wrong with your if statements.
if claimedValue.Value ~= "None" and claimedValue.Value ~= "" then ...
This doesnt run, which indicated that the claimedValue.Value is not equal to either of those value, likely indicating that the plot is already claimed? try printing claimedValue.Value before the if statement runs. Also, what is claimedValue.Value set to from the start?
claimedValue.Value is set to None from the start.
The if statement should work, as it is checking if Owner is not equal to none or “”. Removing the and doesn’t make any difference
I just realised i read the script wrong. You are checking if it is not equal to “None”. and since it is “None” it wont run. did you mean to do claimedValue.Value == "None" or claimedValue.Value == ""?
No. I want it to run if the claimedValue.Value is not equal to None. Whenever someone claims the plot, the claimedValue.Value changes to that player’s username.
Fool! The script runs once, do a property change check. Fool!
UPDATED CODE
print("Button Handler Loaded")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local plot = script.Parent
local purchasedItems = plot:WaitForChild("PurchasedItems")
local claimed = plot:WaitForChild("Claimed")
local tycoons = ReplicatedStorage:WaitForChild("Tycoons")
local solarTycoon = tycoons:WaitForChild("Solar_Tycoon")
local upgradeButtons = solarTycoon:WaitForChild("Upgrade_Buttons")
local floor1Folder = upgradeButtons:WaitForChild("Floor1")
print("Variables Initialized")
warn("Reading Function: clearPurchasedItems")
local function clearPurchasedItems()
for _, children in ipairs(purchasedItems:GetChildren()) do
warn("Destroyed existing objects in ", purchasedItems)
children:Destroy()
print("Destroyed existing objects in ", purchasedItems)
end
end
print("Read Function: clearPurchasedItems")
warn("Reading Function: loadFloor1")
local function loadFloor1()
for _, button in ipairs(floor1Folder:GetChildren()) do
local clone = button:Clone()
clone.Parent = purchasedItems
clone.CFrame = button.CFrame
print("Cloned button ", clone.Name, "into ", purchasedItems)
end
end
print("Read Function: loadFloor1")
claimed:GetPropertyChangedSignal("Value"):Connect(function()
local owner = claimed.Value
if owner ~= "None" then
print("Plot claimed by:", owner)
loadFloor1()
else
clearPurchasedItems()
end
end)
Expected Behavior: Whenever a player claims the tycoon, spawn the buttons of Floor1
Current Behavior: Script does not read the property changed signal
@BatmanSmells132 is this what you were telling me? otherwise I got no clue
1 Like
It doesn’t read the change? Thats really odd, make sure the watch (aka property change event) is made before the change in its value. Where is this script? Try to play with it from the server and do a print if it runs at all, like right after.. make sure this is a server script and there is a server script that is changing the owner value. If you use a local script then only the client will see the change and not the server.