Code works in Roblox Studio but not Ingame

So I had finally finished the basics of my game and went to try it out in the real game.

I tried this first: Local Script working in studio, but not in-game
which leads me to believe that what is happening is the script is firing faster than the model can load.
I also tried these two: Script works in roblox studio but not ingame
Why Does this Script only work in roblox studio

This is a video I got of it; first is roblox studio and second is ingame.

My code broke on line 32 BUT I am pretty sure that it broke not because of the script its in but the other script that actually sets it up. First i’ll share the one with the error and the second is the one I think is actually breaking the game.

--Inside of a bool value inside of StarterCharacterScripts
local summon = game.ReplicatedStorage:WaitForChild("RemoteEvents").Stands:WaitForChild("Summon")
local desummon = game.ReplicatedStorage:WaitForChild("RemoteEvents").Stands:WaitForChild("DeSummon")
local hold = game.ReplicatedStorage:WaitForChild("RemoteEvents").Stands:WaitForChild("Held")
local tweenservice = game:GetService("TweenService")
local finishhold = game.ReplicatedStorage:WaitForChild("RemoteEvents").Stands:WaitForChild("FinishHeld")

summon.OnServerEvent:Connect(function(player) --connects to the summon in start
	if player.Character.Attributes.CanSummon.Value == false then return end
	if player.Name ~= script.Parent.Parent.Name then return end
	local havestand = game.ReplicatedStorage:WaitForChild("Stands"):WaitForChild(script.Parent.Value)
	local visstand = havestand:WaitForChild(script.Parent.Value):Clone()
	local attributes = visstand:WaitForChild("Attributes")
	visstand.Name = player.Name.."'s "..havestand.Name
	visstand.Parent = player.Character
	visstand.PrimaryPart.CFrame = player.Character.PrimaryPart.CFrame
	task.wait(0.3)
	visstand.PrimaryPart.CFrame = player.Character.PrimaryPart.CFrame * CFrame.new(attributes:FindFirstChild("Placement").Value)
	for i, v in pairs(visstand:GetDescendants()) do
		if v:IsA("BasePart") then
			if v.Name:find("In") then
				
			else
				v.Transparency = 0
			end
		end
		if v.Name == "HumanoidRootPart" then
			v.Transparency = 1
		end
	end
	while task.wait(.1) do --link hp
		local userhealth = script.Parent.Parent.Humanoid
		task.wait(1)
		local standhealth = visstand.Humanoid --BREAKS HERE
		userhealth.Changed:Connect(function()
			standhealth.Health = userhealth.Health
		end)
	end
end)

Second:

--inside of a folder inside of the stand which is stored inside of the player.
if script.Parent.Parent.Name:find("'s ") then --if the name has the "'s"
	print("im a clone!")
	deb = true
	if script.Parent.Parent.Name:find("Ghost") then --differentiate between a ghost and real stand fr
		if not script.Parent.Parent.Parent.HumanoidRootPart:FindFirstChild("GhostAttachment") then
			local standplacement = Instance.new("Attachment") --where ghost should be
			standplacement.CFrame = CFrame.new(-0.5,0,0)
			standplacement.Parent = script.Parent.Parent.Parent.HumanoidRootPart
			standplacement.Name = "GhostAttachment"
		end

		local attachghostrn = Instance.new("AnimationConstraint")
		attachghostrn.Parent = script.Parent.Parent.HumanoidRootPart
		attachghostrn.Attachment0 = script.Parent.Parent.HumanoidRootPart.RootAttachment
		attachghostrn.Attachment1 = script.Parent.Parent.Parent.HumanoidRootPart:FindFirstChild("GhostAttachment")
	else
		if not script.Parent.Parent.Parent.HumanoidRootPart:FindFirstChild("StandAttachment") then
			local standplacement = Instance.new("Attachment") --where stand should be
			standplacement.CFrame = CFrame.new(script.Parent.Parent.Attributes.Placement.Value)
			standplacement.Parent = script.Parent.Parent.Parent.HumanoidRootPart
			standplacement.Name = "StandAttachment"
			print("exists!")
		end


		local attachstandrn = Instance.new("AnimationConstraint")
		attachstandrn.Parent = script.Parent.Parent.HumanoidRootPart
		attachstandrn.Attachment0 = script.Parent.Parent.HumanoidRootPart.RootAttachment
		attachstandrn.Attachment1 = script.Parent.Parent.Parent.HumanoidRootPart:FindFirstChild("StandAttachment")
		print("attached!")
	end	
	--------------------------------------------------------------------------------------------------------------------------	
	while task.wait(.1) do --link hp
		local standhealth = script.Parent.Parent.Humanoid
		local userhealth = script.Parent.Parent.Parent.Humanoid
		standhealth.Changed:Connect(function()
			userhealth.Health = standhealth.Health
		end)
	end	
end

1 Like

Oh cool, i’ve been summoned. I think my problem at the time was the code was running before the instances could load (either that or I was being stupid, I didn’t know sh*t at the time). First thing I would try to do is make sure all game assets are loaded before running the script

1 Like

I have no idea how I would do that; the best I could think of was to add a ‘while deb == true do’
but I dunno if that would even work.

Edit: It didn’t

1 Like

I think waiting until game.loaded() == true worked for me

1 Like

Can you show us what is inside “script.Parent.Value”?, if it is a character then try “local standhealth = visstand:WaitForChild(“Humanoid”)” since you said:

1 Like

script.parent.value changes depending on what the player chooses but its what they have. so if someone has the world then when that script fires it gets the world model.

Edit: I forgot to mention I already tried local standhealth = visstand:WaitForChild(“Humanoid”) unfortunately.

1 Like

I originally asking how to accomplish this but then i realised you dont put task.wait but game.Loaded:Connect(function()

Edit: Replacing the top with game.Loaded or just adding it instead of replacing the top makes it never load at all.

1 Like

Can you show us the error in the output?, since it changes dynamically, your other script might’ve changed it to something else other than a character with a Humanoid.

1 Like

I found the problem occurs when the stand is pulled back in because its on a loop and that breaks the loop until fired again so it isn’t a problem anymore. The main problem now is the 2nd script which doesn’t attach the rig to the attachment like it should.

1 Like

You mean it misplaced or it doesn’t follow the attachment?.

1 Like

its supposed to attach with i think either a rigid constraint or a weld constraint (i forgot) but it just falls instead of staying like in the video

1 Like

It’s likely breaking because the stand model or it’s Humanoid doesn’t exist yet (or has already been destroyed) when the script tries to access it. Roblox Studio tends to load things instantly and doesn’t always replicate exactly like a live server, so timing issues can show up only in-game.

You’re also running that health link inside a while loop, which keeps making new connections forever, that can easily cause errors once the stand is removed. Instead of looping, just connect once after confirming the Humanoid exists, e.g.:

local standHum = visstand:WaitForChild("Humanoid")

before using it.

So, all short, double check:

  • The stand clone is fully parented before referencing its Humanoid.
  • You’re not reconnecting signals in a loop.
  • You’re handling the case where the stand or character gets deleted.

Most likely it’s just a replication timing issue that only shows up in live servers, so adding a small WaitForChild() or a short delay before linking health should fix it.

1 Like

Like said it previous comments, I’ve already tried

like in

unless you arent talking about humanoid health. in that case, which part are you talking about?
I am like 90% sure that the problem is in the second script where it isn’t attaching; it just falls straight through the ground. So I tried making it a remote event instead of just running automatically + making it wait one second to make sure everything loads before the remote event fires and it still is broken.

Edit 2: I also updated the 2nd script:

local attach = game.ReplicatedStorage:WaitForChild("RemoteEvents").Stands:WaitForChild("Attach")

attach.OnServerEvent:Connect(function(player)
		print(script.Parent.Parent.Parent.HumanoidRootPart:FindFirstChild("StandAttachment"))
		script.Parent.Parent.HumanoidRootPart.Anchored = false
		if script.Parent.Parent.Name:find("Ghost") then --differentiate between a ghost and real stand fr
		local standplacement = script.Parent.Parent.Parent.HumanoidRootPart.GhostAttachment
		standplacement.CFrame = CFrame.new(-0.5,0,0)
			local attachghostrn = script.Parent.Parent.HumanoidRootPart.AnimationConstraint
			attachghostrn.Attachment0 = script.Parent.Parent.HumanoidRootPart.RootAttachment
			attachghostrn.Attachment1 = standplacement
		else
		local standplacement = script.Parent.Parent.Parent.HumanoidRootPart.StandAttachment
		standplacement.CFrame = CFrame.new(script.Parent.Parent.Attributes.Placement.Value)
			local attachstandrn = script.Parent.Parent.HumanoidRootPart.AnimationConstraint
			attachstandrn.Attachment1 = script.Parent.Parent.Parent.HumanoidRootPart:FindFirstChild("StandAttachment")
			print("attached!")
		end	
		--------------------------------------------------------------------------------------------------------------------------	
		while task.wait(.1) do --link hp
			local standhealth = script.Parent.Parent.Humanoid
			local userhealth = script.Parent.Parent.Parent.Humanoid
			standhealth.Changed:Connect(function()
				userhealth.Health = standhealth.Health
			end)
		end	
end)

And I made everything already appear beforehand.

1 Like

That is the sign of the weld or animation constraint got delete after it welded into some parts, can you try to run the game and check for it inside the Explore tab to see if it’s still in the HumanoidRootPart during runtime?.

1 Like

How do I look at Explore tab while in the game? I haven’t been talking about pressing the “Test” button on roblox studio; I have been talking about the play button to the game.

Edit: Although I still cant look at the explore tag inside of a game, I both turned on the visible feature for the attachments


and made the constraint print attachment1

Hopefully this helps

1 Like

Holy crap I just switched from AnimationConstraints to RigidConstraints and not only did it stop moving the player itself it also fixed the problem. Thank you to Halibon_Candy, DiscMil, and 2xA_Imagineer95 for helping!

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