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

I Managed To Fail This Keybind Script --- How?

The goal of this is to make it so that if you're in a zone, it'll give you an option to open the door by pressing Q. This works fine on Studio, but FAILS on the game. This is my script:

S1 = script.Parent.Sensors.S1
S2 = script.Parent.Sensors.S2
Door = script.Parent.Door
ST = script.Parent.Sensors.SensorTop
SF = script.Parent.Sensors.FloorSensor
_G.ActionGUIs = game.ReplicatedStorage.ActionGUIs

function Animate()
	
end

EntranceDoorDebounce = false
_G["OpenEntranceDoor"] = function ()
	if not EntranceDoorDebounce then
		EntranceDoorDebounce = true
		Door.BodyVelocity.velocity = Vector3.new(0, 2,0)
		ST.Touched:connect(function (hit)
			if hit.Name == "Door" then
				wait(3)
				hit.BodyVelocity.velocity = Vector3.new(0, -2, 0)
				SF.Touched:connect(function (hit)
					if hit.Name == "Door" then
						EntranceDoorDebounce = false
					end
				end)
			end
		end)
	end
end

function DealHotkey(hit)
	if not hit.Parent:FindFirstChild("Humanoid") then return end
	local Player = hit.Parent
	Player.Actions.Action.CanDoAction.Value = true
	Player.Actions.Action.ActionType.Value = "EntranceDoor"
	local newGUI = _G.ActionGUIs.OpenDoorGUI:Clone()
	newGUI.Parent = game.Players:FindFirstChild(Player.Name).PlayerGui
end

function DealHotkey2(hit)
	if not hit.Parent:FindFirstChild("Humanoid") then return end
	local Player = hit.Parent
	Player.Actions.Action.CanDoAction.Value = false
	Player.Actions.Action.ActionType.Value = ""
	local datGUI = game.Players:FindFirstChild(Player.Name)
	if not datGUI.PlayerGui:FindFirstChild("OpenDoorGUI") then return end
	datGUI.PlayerGui.OpenDoorGUI:Destroy()
end

S1.Touched:connect(DealHotkey)
S2.Touched:connect(DealHotkey)
S1.TouchEnded:connect(DealHotkey2)
S2.TouchEnded:connect(DealHotkey2)

And the script that calls the actual door movement is located in the player, this is it:

local player = game.Players.LocalPlayer
local m = player:GetMouse()

m.KeyDown:connect(function (key)
	if key == "q" then
		if script.Action.CanDoAction.Value == false then return end
		if script.Action.CanDoAction.Value == true then
			local ActionType = script.Action.ActionType.Value
			if ActionType == "EntranceDoor" then
				_G.OpenEntranceDoor()
			end
		end
	end
end)

If you know what I've stuffed up, please respond. Thanks in advance.

6 comments

0

Is your second script a LocalScript?

0

Yes, it is.

0

So is the Gui displaying and when you press Q nothing happens or is nothing displaying at all?

0

What is happening is EVERYTHING is working except the actual door movement. I'm also pretty certain it may not be reading the values correctly.

0

Hit detection works, taking the GUI away and putting it in works, but the only thing wrong is the moving of the door.

0

This is the game I'm using it in. http://www.roblox.com/--place?id=104698139

1 answer

✓ Accepted answer

You're trying to use a _G function that was made on the server from the client. Since everything is local in solo mode, this isn't an issue there. However, there are separate script context instances made when a client connects (else your client script would have to wait until they connect to the server to start running).

Basically, your _G function doesn't exist in the context of the LocalScript.

2 comments

0

Ah, so do I have to clone another script and call that one from the LocalScript or do I have to move the whole function over?

0

Ah, nevermind. I just moved the door script over to the LocalScript, now it calls fine. Thanks for your time!

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