So, I'm making a game called Samurai Simulator and I'm trying to make a quest menu. This is my code:
local function onMouseClick(player) local questnum = player:WaitForChild("Quests"):WaitForChild("questnumber") print(questnum.Value) if questnum.Value == 0 then local Quest1 = game.ReplicatedStorage.Quest1 print("click1") Quest1:FireClient(player) elseif questnum.Value == 1 then local Quest2 = game.ReplicatedStorage.Quest2 print("click2") Quest2:FireClient(player) end end script.Parent.ClickDetector.MouseClick:connect(onMouseClick)
There is a folder in Players called Quests and a IntValue called questnumber inside. Quest1 and Quest2 are both events in Replicated Storage and those events open up GUI tabs for both Quest1 and Quest2. Even when the value of questnumber changes, the script still thinks that the value is 0 and Quest2 does not work. Could anyone help? Thanks!
I believe the server doesn't act as if the second quest is the current one since to it, it isn't. One of the two cases are possible in your script's logic as of now:
1. You didn't change questnum
's value at all, neither on the server nor on the client.
2. You change questnum
's value on the client, hence the server observes no change.
Here's the fixed script that adds 1 to questnum
's value, you might want to put the line:
lua
questnum.Value = questnum.Value + 1
in the if-then itself since as of now, the script will advance to quest 3 which you don't have a case for
lua
local function onMouseClick(player)
local questnum = player:WaitForChild("Quests"):WaitForChild("questnumber")
print(questnum.Value)
if questnum.Value == 0 then
local Quest1 = game.ReplicatedStorage.Quest1
print("click1")
Quest1:FireClient(player)
elseif questnum.Value == 1 then
local Quest2 = game.ReplicatedStorage.Quest2
print("click2")
Quest2:FireClient(player)
end
questnum.Value = questnum.Value + 1
end
script.Parent.ClickDetector.MouseClick:Connect(onMouseClick)
On another note, here are some recommendations:
- A way to shorten your if-thens even further:
lua
local function onMouseClick(player)
local questnum = player:WaitForChild("Quests"):WaitForChild("questnumber")
print(questnum.Value)
local remote = game.ReplicatedStorage:FindFirstChild("Quest"..questnum.Value)
remote:FireClient(player)
questnum.Value = questnum.Value + 1
end
script.Parent.ClickDetector.MouseClick:Connect(onMouseClick)
- Using a single remote for all quests since it can be extensive manual work to make a remote for each quest:
lua
local remote = game.ReplicatedStorage:FindFirstChild("Quests")
local function onMouseClick(player)
local questnum = player:WaitForChild("Quests"):WaitForChild("questnumber")
remote:FireClient(player, questnum.Value + 1) -- Also provides a parameter for the quest being started or being attempted to start
questnum.Value = questnum.Value + 1
end
script.Parent.ClickDetector.MouseClick:Connect(onMouseClick)
- Using :Connect()
instead of :connect()
, :connect()
is deprecated and should not be used.
That's it from me! Be sure to comment if you have any questions or I have made a mistake!