What's wrong with my script?

  1. What do you want to achieve?

The RemoteEvent connects to the function every time it’s fired instead of only one time.

  1. What is the issue?

The RemoteEvent connects to the function the first time it’s fired, but the second time it’s fired, it does not connect. I believe that the issue is within the ItemHandler script since that’s where the function/RE is supposed to connect to.

Video:

  1. What solutions have you tried so far?

One of my friends tried to help me but the issue still hasn’t been fixed.

I can also guarantee that the issue is due to or at least is related to the RE not connecting to the function a second time since I’ve used prints to determine this.

Screenshot 1

This is from the second use, the “2” is supposed to be printed after it’s connected to the function.

Screenshot 2

This is from the first use, the “2” is printed after it’s connected to the function.


Workspace Screenshots

Note: DisplayEvent RE (RemoteEvent) is located in ShopEvents folder in ReplicatedStorage.

SpecialDisplay

Note: Ignore the “Num” (I forgot to delete it)

Screenshot

Screen Shot 2023-04-05 at 5.14.12 PM

ShopGUI

Note: object is parented to “Model” within ViewportFrame

Screenshot

Scripts

ProximityPrompt ServerScript

Note: Ignore the - - out code, that was from me messing around with it.

Script
local cooldown = 1800
local canUse = true

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DisplayEvent = ReplicatedStorage.ShopEvents:WaitForChild("DisplayEvent")

--local item = script.Parent.Parent.ObjectDisplay:FindFirstChild()


script.Parent.Triggered:Connect(function(player)
	--task.wait(2)
	if canUse then
		local card_stat = player.Data_Folder.MAX_CARD_STORAGE
		local current_card = player.Data_Folder.CurrentCardStorage

		local art_stat = player.Data_Folder.MAX_ART_STORAGE
		local current_art = player.Data_Folder.CurrentArtStorage


		if current_card.Value >= card_stat.Value or current_art.Value >= art_stat.Value then
			-- Show the delete message for a few seconds
			player.PlayerGui.GameInventory.DeleteOneMsg.Visible = true
			player.PlayerGui.GameInventory.DeleteOneMsg.Msg.Text = "You must remove ONE item from your inventory."

			wait(2)
			player.PlayerGui.GameInventory.DeleteOneMsg.Visible = false

		else
			if current_card.Value < card_stat.Value or current_art.Value < art_stat.Value then
			

				local selected
				for index, value in pairs(script.Parent.Parent.ObjectDisplay:GetChildren()) do 
					if value:IsA("Model") then 
						selected = value
						DisplayEvent:FireClient(player, selected)
						print (value)
						print("displayevent fired")
					end
				end

				player.PlayerGui.GameInventory.DeleteOneMsg.Visible = false
				-- Hide the delete message if it's still visible

				-- Open the card selection GUI
				 local gui = game.ReplicatedStorage.GUIs.SpecialShopGUI:Clone()
				 gui.Parent = player.PlayerGui

			end
		end

		canUse = false
		script.Parent.Enabled = false
		wait(cooldown)
		canUse = true
		script.Parent.Enabled = true
	end
end)

ItemHandler ClientScript

Note: Ignore the - - out code, that was from me messing around with it.

Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DisplayEvent = ReplicatedStorage.ShopEvents:WaitForChild("DisplayEvent")

local function playSound()
	script.AddSound:Play()
end

-- Get the ItemDesc object from the parent of the script
local Info = script.Parent.ItemDesc

DisplayEvent.OnClientEvent:Connect(function(item)
	print("Event")
	
	repeat task.wait() until item --added

-- Loop through each slot in the grid
for i, slot in ipairs(script.Parent.Slots:GetChildren()) do
	-- Check if the slot is a Frame object
	if slot:IsA("Frame") then
			-- Add a MouseEnter event listener to the slot
			local newItem = item:Clone()
			
				newItem.Parent = slot.ViewportFrame.Model
			print(newItem.Name.."parented")
			script.Parent.Slots.Cost.Text = "Coins: ".. newItem:FindFirstChild("Cost").Value --added
			
		slot.MouseEnter:Connect(function()
				
				--local newItem = item:Clone()
				print (newItem)
		
			-- Get the first child of the ObjectDisplay part and set the ItemName object in the Info variable to its Name property
			Info.ItemName.Text = newItem.Name
			Info.ItemDescription.Text = newItem:FindFirstChild("Desc").Value

			-- Clone the first child and set its parent to the ViewportFrame.Model property of the specific slot
		--	newItem.Parent = slot.ViewportFrame.Model
		--	print(newItem.Name.."parented")
			-- Set the Visible property of the Info object to true
			Info.Visible = true
		end)
		
		
		-- Add a MouseLeave event listener to the slot
		slot.MouseLeave:Connect(function()
			-- Set the Visible property of the Info object to false
			Info.Visible = false
		end)
		local slotframe = script.Parent.Slots

		local function update(specific)

			-- Disable and re-enable the RotateVis script under the specific slot's parent
			local rotateVis = slotframe:FindFirstChild(specific).RotateVis
			rotateVis.Disabled = true
			rotateVis.Disabled = false
		end

		--update()
		
		local slots = script.Parent.Slots
		--// glowing selection reset
		slots.Primary.ImageButton.Transparency =1
		
		local equipped = true
		
		for i, slot in ipairs(script.Parent.Slots:GetChildren()) do

			-- Check if the slot is a Frame object
			if slot:IsA("Frame") then
				
	
				-- Add a MouseClick event listener to the slot
				slot.ImageButton.MouseButton1Click:Connect(function(player)
					-- Get the first child of the ViewportFrame.Model and set the itemName variable to its Name property
					local itemModel = slot.ViewportFrame.Model:GetChildren()[1]
					local itemName = itemModel.Name

					local player = game.Players.LocalPlayer
					print(player) -- This will print the value of player in the console
					print(player.leaderstats)

					-- Check if the slot has an item equipped
					local shopitem = itemModel
					local itemcost = shopitem:FindFirstChild("Cost").Value

					if equipped == true and itemcost > player.leaderstats:FindFirstChild("Coins").Value then

						player.PlayerGui.GameInventory.DeleteOneMsg.Visible = true
						player.PlayerGui.GameInventory.DeleteOneMsg.Msg.Text = "You don't have enough coins."
							
						wait(2)
						player.PlayerGui.GameInventory.DeleteOneMsg.Visible = false

					else

						if equipped == true and itemcost <= player.leaderstats:FindFirstChild("Coins").Value then

							local event = game.ReplicatedStorage.InvEvents.PickUp
							local purchaseitem = game.ReplicatedStorage.ShopEvents.Purchase	
							local item = itemName
							local firstChild = itemModel
							local itemdescription = firstChild:FindFirstChild("Desc").Value
							event:FireServer(item, itemdescription)
							purchaseitem:FireServer(itemcost)	

							-- Set the Equipped value of the item to false and hide the item in the slot
							equipped = false
							slot.Visible = false


							-- Clear the model of the slot to remove the item from the slot
							slot.ViewportFrame.Model:ClearAllChildren()

							-- Update the value in the settings for the slot
							if slot == slotframe.Primary then
								script.Parent.Settings.Primary.Value = ''
							end
							end
						-- Play a sound
						playSound()

						script.Parent.Parent.Parent:Destroy()
						end
				end)
			end
		end
		end
		end
end)

I really appreciate your help, you all are extremely talented and smart :heart:
Also, if you’d prefer to do TeamCreate, please let me know.

Wouldn’t it be best to use print statements to narrow down suspicions?

Just do print("work") and then print("work 1"), `print(“work 2”), and so on for each statement or function.

1 Like

I have used prints to determine that it indeed is an issue with the ItemHandler script and that the issue is that it isn’t being connected when the RE is fired.

Ah, I didn’t notice that part ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎

1 Like

In your ProximityPrompt server script, you put the
DisplayEvent:FireClient(player, selected)

in a for loop so I guess if there was more than one model in script.Parent.Parent.ObjectDisplay and therefore DisplayEvent fires client more than once. I don’t know if there are other scripts that might add models into ObjectDisplay, so try keeping an eye on the children of ObjectDisplay in the explorer while you’re testing.

In your ItemHandler client script, you put
print("Event")
right after
DisplayEvent.OnClientEvent:Connect(function(item)
[/quote] but in your screenshot 2 (first use) the word “Event” was not printed, so I supposed you added that print afterwards? If you tried testing again and the word “Event” is printed but everything else after that is not printed then it is probably because of the repeat loop.

(Sorry if what I said didn’t help, this is my first reply on devforum)

Maybe try putting a print statement in your proximity server script before your if statement:

print("canUse", canUse)

Might show if it is really triggered but just failing the IF statement.

1 Like

Wow! I’m happy to be your first reply on the DevForum :slight_smile:

In regard to the ProximityPrompt script, there is only one model in ObjectDisplay—so I don’t think it’s firing more than once. There is a serverscript that parents a randomized model based on rarity to the ObjectDisplay from a folder in ReplicatedStorage named “Shop”.

In the ItemHandler script, this screenshot is from the test I’ve just did and it’s the same results.

Thank you!

Thank you for the idea. Here’s the output:

Uh did you destroy this script along with its parent?

1 Like

Yes, each time the proximity prompt is triggered, then the GUI is cloned into PlayerGUI and destroyed when the player selects the item or closes the GUI. I’ve done this because:

  1. it doesn’t screw with the InventoryGUI.
  2. so the item in the ViewportFrame GUI can be reset for the second use.

But the GUI was cloned after the DisplayEvent:FireClient(player, selected) so at the time the event fires the client there was no GUI and its script to perform DisplayEvent.OnClientEvent:Connect(function(item).

Try putting the clone GUI part of script into the for loop together with the DisplayEvent:FireClient(player, selected)?

Edit: Advice

1 Like

Oh my goodness!!! You’ve solved it!! :partying_face: :scream: :exploding_head: (99.9% sure)
EDIT: 100% sure!

	else
			if current_card.Value < card_stat.Value or current_art.Value < art_stat.Value then
				
				-- Open the card selection GUI
				local gui = game.ReplicatedStorage.GUIs.SpecialShopGUI:Clone()
				gui.Parent = player.PlayerGui

				local selected
				for index, value in pairs(script.Parent.Parent.ObjectDisplay:GetChildren()) do 
					if value:IsA("Model") then 
						selected = value
						DisplayEvent:FireClient(player, selected)
						print (value)
						print("displayevent fired")
					end
				end

				player.PlayerGui.GameInventory.DeleteOneMsg.Visible = false
				-- Hide the delete message if it's still visible

			end
		end

		canUse = false
		script.Parent.Enabled = false
		wait(cooldown)
		canUse = true
		script.Parent.Enabled = true
	end
end)
1 Like

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