Hey all, I have a pretty simple question about a certain script in my game. Basically, I am making a “Tycoon-esque” game, and I am definitely a beginner coder, so I am attempting to reason my way through the creation process and attempting to solve errors when I find them.
I have gotten this far: Creation of the tycoon, one upgrade, and using ProfileService to store all tycoon/money values.
The issue I was having is the way I dealt with the first upgrade. When a player spawns in their tycoon, it creates a part with a touch event (activated from another script)
The code in a LocalScript:
local Upgrade1 = workspace[UserId].baseHome:WaitForChild("upgrade1")
Upgrade1.Touched:Connect(function(otherPart)
if otherPart.Parent == game.Players.LocalPlayer.Character then
if not debounce then
debounce = true
UpgradeEvent1:FireServer()
else
wait(1)
debounce = false
end
end
end)
The remote event links to this ServerScript::
UpgradeEvent1.OnServerEvent:Connect(function(player)
local Upgrade1 = workspace[player.UserId].baseHome:FindFirstChild("upgrade1")
local playerProfile = profileCache[player]
if playerProfile.Data.Money >= 15 then
playerProfile.Data.Money-=15
local firstUpgrade = ReplicatedStorage["home upgrades"]:FindFirstChild("upgrade1"):Clone()
firstUpgrade.Parent = workspace[player.UserId]:FindFirstChild("baseHome")
firstUpgrade:PivotTo(Upgrade1:GetPivot() + Vector3.new(15, .875, 9))
Upgrade1:Destroy()
for _, house in workspace[player.UserId]:GetChildren() do
if house.Name == "baseHome" then
playerProfile.Data.home = RBLXSer.Encode(house, true)
end
end
else
print("not enough money!")
end
end)
All of this actually works for mt game, but in the server code, I serialize the new tycoon model and save that into the PlayerService datastore for use the next time that same player joins and spawns their updated tycoon.
However, as the above LocalScript is in StarterPlayerScripts, if a player owns an updated Tycoon, I am met with this error:
Touched is not a valid member of Model “Workspace.40358879.baseHome.upgrade1”
I believe this is because “Upgrade1” is destroyed upon buying it, and will not be spawned in again if the player “owns” it.
As I stated earlier, this does not seem to affect any of actual gameplay, but I wanted to check with other people to see if this should be a cause for concern.
What solutions I have tried?
I attempted to add an if statement at the beginning of the LocalScript code to check if the part exists, however it did not work
if Upgrade1 ~= nil then
-- run the touched event
else
-- do not run the event
end
Any information would be great on this, so I appreciate the help. In addition, I am also getting this:
“Infinite yield possible on 'Workspace.40358879:WaitForChild(“baseHome”)”
It also does not affect the code, but if anyone thinks this is a cause for concern, let me know. Thanks!