Help needed with bush hiding script

Hello all and hope everyone is doing great. I’m fairly new to scripting, still consider myself a script kitty and want to make a horror game with some of my acquired knowledge. Had a few issues and eventually got past them but this one script for my bush seems to have me stumped. Been waiting to be able to post on here and maybe get some helpful feedback and possibly an answer to my problem. Thank you all for taking the time to read my post and looking into my problem.

You can write your topic however you want, but you need to answer these questions:

  1. I’m trying to make a bush for any player to hide in within this horror experience.

  2. The issue is the player cannot hide back in the same bush. So the way the bush works it when close to the bush, the player can use the proximity prompt to enter the bush and once they start moving forward or any direction a few studs they come out of hiding. Problem is mainly the proximity prompt does not re-enable allowing the user to hide back in the bush they just left.

  3. I’ve tried looking for solutions on scriptures help and using the robot (Chatgpt) to help find a solution but unfortunately neither could give a good answer. Even decided to go a different route for how I would make the bush work but really don’t wanna give up on my script.

Here is the code for the hiding bush, also the script is in a folder in the workspace which contains the bush models I am using:

local replicatedStorage = game:GetService("ReplicatedStorage")

local event = replicatedStorage:WaitForChild("HideEvent")
local bush = script.Parent:GetChildren()

-- Function to check if the player moved and set "Hiding" to false if they did
local function checkPlayerMovement(player)
	local character = player.Character
	if not character then return end

	local initialPosition = character.PrimaryPart.Position

	while player and player.Parent do
		task.wait(1)  -- Adjust the frequency of checking as needed

		local currentPosition = character.PrimaryPart.Position
		local distance = (currentPosition - initialPosition).Magnitude

		if distance > 1 then  -- Adjust the threshold as needed
			player.Hiding.Value = false
			break
		end
	end
end

for i, v in pairs(bush) do
	if v:IsA("Model") then
		local playerHiding = nil
		local prompt = v.PromptPart.Prompt

		prompt.Triggered:Connect(function(player)
			local character = player.Character
			if not character then return end

			if not playerHiding then
				prompt.Enabled = false
				playerHiding = player
				player.Hiding.Value = true

				character:PivotTo(v.InsidePos.CFrame)
				character.Humanoid.WalkSpeed = 0
				character.Humanoid.JumpHeight = 0

				event:FireClient(player, v)

				task.wait(0.5)
				

				event.OnServerEvent:Connect(function(player, model)
					if model == bush then
						local character = player.Character
						if character and character.Humanoid.Health > 0 then
							character:PivotTo(v.OutsidePos.CFrame)
							character.Humanoid.Walkspeed = 12
							character.Humanoid.JumpHeight = 7.2

							prompt.Enabled = true
							playerHiding = nil
							player.Hiding.Value = false
						end
					end
				end)

				-- Start checking player movement
				spawn(function()
					checkPlayerMovement(playerHiding)
				end)
			end
		end)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local hiding = Instance.new("BoolValue", player)
	hiding.Name = "Hiding"
	hiding.Value = false

	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			if player.Hiding and player.Hiding.Value == true then
				player.Hiding.Value = false
			end
		end)
	end)
end)

If the player is stuck due to the proximity prompt not re-enabling movement, there should be a simple way around this.

Just have a BoolValue in the bush (playerHiding) and make it so the proximity prompt changes that value. When the player moves the value is changed again.

(I might have misread your post but this still should work. ALSO, it seems like you are overcomplicating this a lot)

1 Like

I appreciate your response. The player isn’t stuck, they can move as much as they want. The problem is, the player is unable to hide again within the same bush. Sorry if I phrased my original post in a way that suggest the player is stuck and unable to move.

Also I am open for suggestions to simplify this and if this code seems over complicated. LOL I just want to make it so the player is able to hide in the bush at any time and within the same bush if it was just used from the monsters I have in game.

Thanks for all the help, I figured it out. Just used some touched events and made it so when certain parts are touched the player would have a bool value that changes making it so the npc doesn’t chase them and vice versa when they touch another part that makes the value set back to false.

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