Hive save/load code not letting use 2nd time

So i was creating a bee swarm simulator fangame
the problem is…
well, whenever i leave (chat, because im still testing) the game, i can no longer use the hive.
thank god i did testing there.

my code:

script.Parent.Text = ""
	script.Parent.Parent.Parent.Attachment.ProximityPrompt.Triggered:Connect(function(Player)
		if not Player:HasTag("HasHive") then
			Player:AddTag("HasHive")
			local HiveValue = Instance.new("ObjectValue")
			HiveValue.Parent = Player
			HiveValue.Value = script.Parent.Parent.Parent.Parent
			script.Parent.Text = Player.Name
			script.Parent.Parent.Parent.Attachment.ProximityPrompt.Enabled = false
			game.ReplicatedStorage.CollectBees:FireClient(Player)
			game.ReplicatedStorage.ShowBees.OnServerEvent:Connect(function(Plr, Hive)
				if Plr:HasTag("HasHive") and Plr == Player then
					for i, obj in pairs(script.Parent.Parent.Parent.Parent.Slots:GetChildren()) do
						if obj:IsA("UnionOperation") then
							if obj:FindFirstChild("BeeType") then
								obj.BeeType.Value = game.ServerStorage.Bees:WaitForChild(Hive[i])
								print(obj.BeeType.Value)
							end
						end
					end
				end
			end)
			Player.Chatted:Connect(function()
				script.Parent.Text = ""
				script.Parent.Parent.Parent.Attachment.ProximityPrompt.Enabled = true
				for i, obj in pairs(script.Parent.Parent.Parent.Parent.Slots:GetChildren()) do
					if obj:IsA("UnionOperation") then
						if obj:FindFirstChild("BeeType") then
							obj.BeeType.Value = nil
						end
					end
				end
			end)
		end
	end)
2 Likes

Could you elaborate on what the problem is? I’m having trouble understanding what’s supposed to happen, and what’s not.

This is not how you use RemoteEvents. They shouldn’t be connected to inside of other connections if you’re not disconnecting them, as this is almost always not the desired behavior.

This script shouldn’t be directly inside the Hive model, as this would require the same script to be duplicated and put in all the hives. Instead, put the script in ServerScriptService (SSS)! You’d need to loop through all the hives and implement similar logic to get it to work after moving the script to SSS.

Consider making variables to make your code more readable! For example:

for _, hive in pairs(workspace.Hives:GetChildren()) do
    local ui = Hive.SurfaceGui.Container.TextLabel
    local prompt = Hive.Attachment.ProximityPrompt
    -- rest of script
end
2 Likes

i am NOT making it more readable.
anyway, the problem is, whenever i try to claim it a second time, the hive claiming instantly gets aborted, and the prompt stays.
this is not intended.
how may i fix it?
also heres my clutter of events:
gift

2 Likes

Basic readability should always be maintained if possible. It is quite simply very hard for anybody to help you at the moment because your code is very hard to read. Usually it is also not a wise idea to create huge chains of .Parent statements as indexing Instances in your workspace by name will give people an idea of what you are actually referencing. Blueberry has also already mentioned other things you should definitely consider.

Regardless of that I don’t quite get the original problem here. What exactly do you mean with you can’t use the hive when somebody leaves? Why would somebody need to use a hive when they leave the game. It would be helpful if you provide some extra context and give a detailed explanation of what exactly you are trying to do.

3 Likes

did you remove the player tag: HasHive ?

2 Likes

let me try it, may fix it even tho i did it in the sidebar

1 Like

mostly fixed buuuuuuuuuuuuut
the print statements multiply each time…

1 Like

your putting this function inside of a function that happens with the proximityprompt is triggered,

game.ReplicatedStorage.ShowBees.OnServerEvent:Connect(function(Plr, Hive)
				if Plr:HasTag("HasHive") and Plr == Player then
					for i, obj in pairs(script.Parent.Parent.Parent.Parent.Slots:GetChildren()) do
						if obj:IsA("UnionOperation") then
							if obj:FindFirstChild("BeeType") then
								obj.BeeType.Value = game.ServerStorage.Bees:WaitForChild(Hive[i])
								print(obj.BeeType.Value)
							end
						end
					end
				end
			end)

put it outside of the triggered function or else it stacks up.

2 Likes

now it no longer loads bees

script.Parent.Text = “”
game.ReplicatedStorage.ShowBees.OnServerEvent:Connect(function(Plr, Hive)
if Plr:HasTag(“HasHive”) and Plr:FindFirstChild(“HiveValue”) and Plr.HiveValue.Value == script.Parent.Parent.Parent.Parent then
for i, obj in pairs(script.Parent.Parent.Parent.Parent.Slots:GetChildren()) do
if obj:IsA(“UnionOperation”) then
if obj:FindFirstChild(“BeeType”) then
obj.BeeType.Value = game.ServerStorage.Bees:WaitForChild(Hive[i])
print(obj.BeeType.Value)
end
end
end
end
end)
script.Parent.Parent.Parent.Attachment.ProximityPrompt.Triggered:Connect(function(Player)
if not Player:HasTag(“HasHive”) then
Player:AddTag(“HasHive”)
local HiveValue = Instance.new(“ObjectValue”)
HiveValue.Parent = Player
HiveValue.Value = script.Parent.Parent.Parent.Parent
script.Parent.Text = Player.Name
script.Parent.Parent.Parent.Attachment.ProximityPrompt.Enabled = false
game.ReplicatedStorage.CollectBees:FireClient(Player)
Player.Chatted:Connect(function()
Player:RemoveTag(“HasHive”)
script.Parent.Text = “”
script.Parent.Parent.Parent.Attachment.ProximityPrompt.Enabled = true
for i, obj in pairs(script.Parent.Parent.Parent.Parent.Slots:GetChildren()) do
if obj:IsA(“UnionOperation”) then
if obj:FindFirstChild(“BeeType”) then
obj.BeeType.Value = nil
end
end
end
end)
end
end)
1 Like

yea idk about printing a lot part

2 Likes

nvm i fixed it :smiley:

script.Parent.Text = ""
game.ReplicatedStorage.ShowBees.OnServerEvent:Connect(function(Plr, Hive)
	if Plr:HasTag("HasHive") and Plr:FindFirstChild("HiveValue") and Plr.HiveValue.Value == script.Parent.Parent.Parent.Parent then
		for i, obj in pairs(script.Parent.Parent.Parent.Parent.Slots:GetChildren()) do
			if obj:IsA("UnionOperation") then
				if obj:FindFirstChild("BeeType") then
					obj.BeeType.Value = game.ServerStorage.Bees:WaitForChild(Hive[i])
					print(obj.BeeType.Value)
				end
			end
		end
	end
end)
	script.Parent.Parent.Parent.Attachment.ProximityPrompt.Triggered:Connect(function(Player)
		if not Player:HasTag("HasHive") then
			Player:AddTag("HasHive")
			local HiveValue = Instance.new("ObjectValue")
			HiveValue.Name = "HiveValue"
			HiveValue.Parent = Player
			HiveValue.Value = script.Parent.Parent.Parent.Parent
			script.Parent.Text = Player.Name
			script.Parent.Parent.Parent.Attachment.ProximityPrompt.Enabled = false
			game.ReplicatedStorage.CollectBees:FireClient(Player)
			Player.Chatted:Connect(function()
				Player:RemoveTag("HasHive")
				script.Parent.Text = ""
				script.Parent.Parent.Parent.Attachment.ProximityPrompt.Enabled = true
				for i, obj in pairs(script.Parent.Parent.Parent.Parent.Slots:GetChildren()) do
					if obj:IsA("UnionOperation") then
						if obj:FindFirstChild("BeeType") then
							obj.BeeType.Value = nil
						end
					end
				end
			end)
		end
	end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.