Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Enabling script through part doesn't work?

I was making a script that would disable the player from moving whenever they touched a part. I tested the script, and it works, but whenever I disable the script it doesn't run whenever I touch the part.

Here is the script for disabling walking:

while true do
	wait()
	local movement = game:GetService("ContextActionService")
	movement:UnbindAction("backwardMovement")
	movement:UnbindAction("leftMovement")
	movement:UnbindAction("rightMovement")
	movement:UnbindAction("forwardMovement")
end

And the script inside the part:

script.Parent.Touched:connect(function()
	game.StarterPlayer.StarterCharacterScripts.Nowalk.Disabled = false
end)

If you have any ideas please let me know. I'm new to scripting, so please explain what I did wrong.

3 comments

0

As a note first, I think the while loop is unneeded. Once unbinded, I don't believe CAS auto-rebinds your movement :P As for your problem, is your game filtering enabled?

0

I tried switching the filtering on and off, it didn't seem to work for either one :/

0

When the script disabled, it is not supposed to run unless you have another script which will enable it in game.

1 answer

✓ Accepted answer

In order for the server to tell the client what needs to happen when you touch the part, you need a remote event/function. In this case, I'll use an event, since you don't need the client to tell the server something back.

Server Script (inside part):

local part = script.Parent;
local event = Instance.new("RemoteEvent");
event.Name = "Stop Movement";
event.Parent = game:GetService("ReplicatedStorage");

local debounce = false;
part.Touched:Connect(function(h)
	if(not debounce) then
			debounce = true;
		if(h.Parent:FindFirstChild("Humanoid")) then
			local c = h.Parent;
			local player = game.Players:GetPlayerFromCharacter(c);
			if(player) then
				event:FireClient(player);
			end
		end
		debounce = false;
	end
end)

Client Script (inside StarterPlayerScripts)

local stopMovement = game:GetService("ReplicatedStorage"):WaitForChild("Stop Movement");
local movement = game:GetService("ContextActionService")

stopMovement.OnClientEvent:Connect(function()
	movement:UnbindAction("backwardMovement");
	movement:UnbindAction("leftMovement");
	movement:UnbindAction("rightMovement");
	movement:UnbindAction("forwardMovement");
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.