0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Robloxで他のスクリプトに書かれた関数を呼び出したい

Posted at

やりたいこと

複数のPartのScriptから同じ関数を呼びたいという目的です。そうすれば、各PartのScriptに同じ関数を書かなくていいから。
今回は、DisplayPartにSetValueという関数を書いておいて、ButtonPartからそのSetValue関数を使います。
参考

構成

image.png

手順

  1. DisplayPartの下にBindableFunctionを追加する。
  2. BindableFunctionの名前を変更する。今回はSetValueFunction。
  3. SetValueFunctionの下にScriptを追加する
  4. ButtonPart.ButtonScriptから、displayPart.setValueFunction:Invoke(1) とかくと、これが使える

コード

setValueFunction
local bindableFunction = script.Parent
local displayPart = bindableFunction.Parent

wait(1)

local function setValue(intValue)
	displayPart.answerValue.Value += intValue
end

bindableFunction.OnInvoke = setValue

ButtonPart.ButtonScript
local ButtonPart =  script.Parent
local clickdetector = ButtonPart.ClickDetector
local displayPart = workspace.DisplayPart
local colorMemory = ButtonPart.BrickColor
local MY_NUMBER = 1

--先に宣言しておかないといけない
local function countMouseClick(player)
	print("1")
	ButtonPart.BrickColor = BrickColor.new(0.7,0.9, 0.9)
	displayPart.setValueFunction:Invoke(1)  --他の関数に記載されている関数を呼び出す
	wait(0.2)
	ButtonPart.BrickColor = colorMemory
end

wait(1)
--初期化中はanswerValueを叩かれると困る

clickdetector.MouseClick:Connect(countMouseClick)

重要なのは、displayPart.setValueFunction:Invoke(1)のみ。
ほかは今回関係ないです。

最後に

これで、他のScriptに書かれた関数をつかう事ができました!便利ですね!
image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?