How to make a generator

im trying to make a generator like in forsaken but it won’t work.
i have a prompt and a front part, it is always checking if the front part is being touched and setting a value to true, then if you use the prompt if the value is true it puts you in front of the generator and starts an animation, and if you move it ends the animation.

local prompt = script.Parent.Use.ProximityPrompt

local plr = game.Players.LocalPlayer

local front = script.Parent.FrontSide
front.Touched:Connect(function() end) -- Just for a TouchInterest to occur on a CanCollide false part

local infront = false

local slashanim = Instance.new("Animation")
slashanim.AnimationId = "rbxassetid://128175105099480"
local anim = plr.Character.Humanoid:LoadAnimation(slashanim)

spawn(function()
	while true do
		wait()
		local touching = front:GetTouchingParts()
		for i=1,#touching do
			if touching[i].Name == "HumanoidRootPart" then
				infront = true
			end
		end
		infront = false
	end
end)

spawn(function()
	while true do
		wait()
		if plr.Character.Humanoid.MoveDirection.Magnitude ~= 0 then
			anim:Stop()
		end
	end
end)

script.Parent.Use.ProximityPrompt.Triggered:Connect(function()
	if infront == true then
		plr.Character.HumanoidRootPart.CFrame = script.Parent.FrontSide.CFrame
		anim:Play()
		if plr.Character.Survivor.Value == true then
			plr.Character.HumanoidRootPart.CFrame = script.Parent.FrontSide.CFrame
			anim:Play()
		end
	end
end)

this is in a local script.

2 Likes

Instead of checking the touching parts (approx.) every frame, why don’t you use the .Touched event to detect a touch start and remove them from the touching if the .TouchEnded event fires and :GetTouchingParts doesn’t contain the part that triggered to .TouchEnded event.


Now, let’s look at your script. First of all, I don’t see the reason to be using a LocalScript. You should migrate everything over to a server Script. This Script can either be in ServerScriptService and handle ALL of the generators, or you could have one script for each generator.


And can you elaborate??? I have NO clue what you mean by

What’s happening? Anything in the output? Is it just not doing anything at all? We need information to help you! I don’t know why so many people just say

i will see if this works.

the code just doesn’t do anything, it doesn’t print errors, it doesn’t make anything happen, maybe the touching thing doesn’t work.

Your logic resets infront to false every loop.. Also GetTouchingParts does not detect character parts unless the part is CanTouch and CanCollide.. false parts won’t register touches. Your animation logic has a duplication. It only needs to play once if the player is in front and survivor is true.

Put that to the test.. Let’s see here:

local prompt = script.Parent.Use.ProximityPrompt
local plr = game.Players.LocalPlayer
local front = script.Parent.FrontSide
front.Touched:Connect(function() end)

local infront = false

local anim = plr.Character.Humanoid:LoadAnimation(Instance.new("Animation", script))
anim.AnimationId = "rbxassetid://128175105099480"

task.spawn(function()
	while true do
		task.wait()
		local touching = front:GetTouchingParts()
		local found = false
		for i=1,#touching do
			if touching[i].Name == "HumanoidRootPart" then
				found = true
			end
		end
		infront = found
	end
end)

task.spawn(function()
	while true do
		task.wait()
		if plr.Character.Humanoid.MoveDirection.Magnitude > 0 then
			anim:Stop()
		end
	end
end)

prompt.Triggered:Connect(function()
	if infront and plr.Character.Survivor.Value then
		plr.Character.HumanoidRootPart.CFrame = front.CFrame
		anim:Play()
	end
end)

See what this does.

The rest depends on how your generator is set up and whether FrontSide is configured to actually register touches. --test print that to be sure.

so i used this and it still does nothing, i tried adding prints to the found = true part and the part where it triggers, and when i touch the part it doesn’t print anything at all.

(also i tried to put it in a server script but i can’t figure out how to translate it so it works in server script)

Hmm..

local prompt = script.Parent.Use.ProximityPrompt
local plr = game.Players.LocalPlayer
local front = script.Parent.FrontSide

local infront = false

front.Touched:Connect(function(hit) print(hit)  --print test
	if hit.Name == "HumanoidRootPart" then
		infront = true
	end
end)

front.TouchEnded:Connect(function(hit)
	if hit.Name == "HumanoidRootPart" then
		infront = false
	end
end)

local anim = plr.Character.Humanoid:LoadAnimation(Instance.new("Animation", script))
anim.AnimationId = "rbxassetid://128175105099480"

task.spawn(function()
	while true do
		task.wait()
		if plr.Character.Humanoid.MoveDirection.Magnitude > 0 then
			anim:Stop()
		end
	end
end)

prompt.Triggered:Connect(function()
	if infront and plr.Character.Survivor.Value then
		plr.Character.HumanoidRootPart.CFrame = front.CFrame
		anim:Play()
	end
end)

I feel like if this was in a server script and it was edited up a bit it might work because i think touched only works on a server script. problem is im tired rn and i have no idea how to do that.

As @2112Jay asked, does the FrontSide Part have the CanTouch property set to false?

You fire the Touched event at the beginning, but then use GetTouchingParts?
If the Touched event never fires then it’s never going to work, so you need to find out where it isn’t working, not just guess.
Use prints to troubleshoot.

task.spawn(function()
    print("Touched event fired")  -- lets you know if the touched event actually is firing
	while true do
		task.wait()
		local touching = front:GetTouchingParts()
		local found = false
		for i=1,#touching do
            print(touching.Name)   -- tells you what is touching FrontSide
			if touching[i].Name == "HumanoidRootPart" then
				found = true
			end
		end
		infront = found 
	end
end)

can touch is on, its on by default and i have no reason to turn it off.

also as i said, i think it has to be in a server script, but idk how to translate all that to a server script,

How about preserveing a player working on each generator?
This script should be in serverscript.