Script only works in "Run" mode and not "Play" mode

so for some reason my script works in run mode but not play mode, i tested out the same exact script, tool, and water in a different game and it works but not in my current game.


I already tried looking on the devforum for other solutions and I only found 2 other posts with this issue and it didnt work.

1 Like

ill also provide the scripts. my code uses a module script and a serverscript so ill send the module first:

local FloatingModule = {}

function FloatingModule.SetupFloating(handle, waterPart)
	local isTouchingWater = false

	local function updateFloating()
		-- Check if the tool is equipped in a character
		local tool = handle.Parent
		local character = tool.Parent
		if character and character:IsA("Model") and character:FindFirstChild("Humanoid") then
			-- Tool is equipped, destroy BodyPosition if it exists
			local bodyPosition = handle:FindFirstChild("BodyPosition")
			if bodyPosition then
				bodyPosition:Destroy()
			end
			return
		end

		-- Normal floating logic
		if isTouchingWater then
			if not handle:FindFirstChild("BodyPosition") then
				local bodyPosition = Instance.new("BodyPosition")
				bodyPosition.Parent = handle
				bodyPosition.Position = handle.Position
				bodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			end

			local bodyPosition = handle.BodyPosition
			local pos = handle.Position * Vector3.new(1, 0, 1)
			pos = pos + Vector3.new(0, waterPart.Position.Y + waterPart.Size.Y / 2, 0)
			bodyPosition.Position = pos
		else
			local bodyPosition = handle:FindFirstChild("BodyPosition")
			if bodyPosition then
				bodyPosition:Destroy()
			end
		end
	end

	handle.Touched:Connect(function(otherPart)
		if otherPart == waterPart then
			isTouchingWater = true
		end
	end)

	handle.TouchEnded:Connect(function(otherPart)
		if otherPart == waterPart then
			isTouchingWater = false
		end
	end)

	game:GetService("RunService").Heartbeat:Connect(function()
		updateFloating()
	end)
end

return FloatingModule

heres my serverscript

local FloatingModule = require(script.FloatingModule)
local waterPart = script:WaitForChild("WaterPart").Value

local function setupFloatingForAllTools()
	local dropsFolder = game.Workspace:WaitForChild("Drops")
	print("Number of tools in Drops:", #dropsFolder:GetChildren())

	for _, tool in ipairs(dropsFolder:GetChildren()) do
		local handle = tool:FindFirstChild("Handle")
		if handle then
			print("Setting up floating for tool:", tool.Name)
			FloatingModule.SetupFloating(handle, waterPart)
		else
			print("Warning: Tool", tool.Name, "does not have a Handle part.")
		end
	end
end

print("Setting up floating for all tools in Workspace.Drops...")
setupFloatingForAllTools()
print("Setup complete for all initial tools.")

game.Workspace.Drops.ChildAdded:Connect(function(newTool)
	print("New tool added to Drops:", newTool.Name)
	local handle = newTool:WaitForChild("Handle")
	if handle then
		print("Setting up floating for new tool:", newTool.Name)
		FloatingModule.SetupFloating(handle, waterPart)
	else
		print("Warning: New tool", newTool.Name, "does not have a Handle part.")
	end
end)

print("Ready to handle new tools added to Workspace.Drops.")

What is the script’s RunContext?

image

the scripts runcontext is legacy

1 Like

I genuanelly don’t know what is causing your issue, but you could try changing the RunContext to Server

Also if it doesn’t work only on your current game, try and move your entire game to a different file

alright i tried t hat and it still doesnt work when i play but works on run

1 Like

maybe ill try recoding it and making it just a serverscript and no module

ok i did that and it still only works in run mode

ok so i published the game and tested the game on roblox instead of studio and it works i think,
must be a studio bug, but it doesnt work in studio even after restarting it?

Sometimes problems like this will come up, but specifically in studio. I’ve encountered things like this plenty of times before.

The reason it works in Run mode is because that’s how the server is supposed to behave at runtime, before any players connect. In studio, when you test in Play mode, your player will join the game ALMOST instantly and begin loading your character and other assets before it realistically should. This doesn’t happen in a real game server, which is why you may never encounter this issue for real. Sometimes it doesn’t really make sense how that would impact things, but it does.

Something I’d suggest is add a script to wait before un-anchoring the axe, a wait of around 3 seconds or so should be fine. This should more realistically represent what will actually happen when your game is run.

It’s not really a solution, but just a confirmation of that being the issue and that it should be unique to testing in studio.

2 Likes

okay so i added that script to make it unanchor after 3 seconds and it still only works in run mode, any idea on how i could fix this? when youve encountered this issue howd you get around it? its just weird to me how it works normal when i play the game using the roblox website and not in studio

could it be related to my script? cause it seems to only be impacting my one script for some reason

Can you do me a favor and print out isTouchingWater (add your own prints to see if there is something off in the module itself as well).

Edit: Also check the explorer if your BodyPosition is being added and set properly in both scenarios.

Honestly, janky engine witchcraft like this scares me. I’m really not sure what steps I could recommend you take to fix this for you, but at least you can take solace in knowing that this issue is probably (hopefully) specific to playtesting. You should try following @OniiSamaUwU’s advice to find more info as to what might be going on.

Okay so it adds the BodyPosition in the handle of the part in run mode, but not in play mode. I have some debugging already set up in my scripts, and it prints everything correctly:

  17:56:23.002  Setting up floating for all tools in Workspace.Drops...  -  Server - FloatingModuleHandler:19
  17:56:23.002  Number of tools in Drops: 2  -  Server - FloatingModuleHandler:6
  17:56:23.002  Setting up floating for tool: Stone  -  Server - FloatingModuleHandler:11
  17:56:23.002  Setting up floating for tool: Axe  -  Server - FloatingModuleHandler:11
  17:56:23.003  Setup complete for all initial tools.  -  Server - FloatingModuleHandler:21

Ill try to add even more debugging like when the body position is being added and when its removed

Then the initial Touched event is probably not firing because before the system gets to connecting the signal, the Handle is already touching the water and wont fire until the Handle fully stops touching it. Use GetPartsInPart to get the initial isTouchingWater value.

2 Likes

Okay so I didnt use GetPartsInPart and instead used GetTouchingParts instead of using touch and touch ended and it seems to work now, I was initially using get touching parts but i couldnt get it to work so i instead used touch and touch ended, reason i was using get touching parts is cause touch and touch ended can be buggy and not work sometimes in my experiences and i was right, it made me do all of this unnecessary stuff.

1 Like

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