So, basically I’ve been trying to make it so when a player touches the given part, the EnableMouseLockOption boolean is set to true. I used a remoteFunction to do this, so when a player touches the part, the client is invoked and the enablemouselockoption property of starterplayer is set locally.
-- Script is in serverscriptservice
local ld = workspace.LockDetect
local rs = game:GetService("ReplicatedStorage")
local mouseLock = rs.MouseLock
ld.Touched:Connect(function()
print("er") -- this line of code works
mouseLock:InvokeClient() -- the issue comes on line 9, issue isnt specified.
end)
-- local script in starterplayer [i didnt know where to put it, suggestions would be appreciated]
local rep = game:GetService("ReplicatedStorage")
local ml = rep.MouseLock --MouseLock is a remote function, issue sometimes comes on this
local sps = script.Parent
ml.OnClientInvoke:Connect(function() -- issue sometimes comes on this line, again not specified
print("??")
sps.EnableMouseLockOption = true
end)
I looked at the documentation, maybe I’m not that smart at research but i couldnt find solutions to this
Like I said, the output doesnt highlight the issue. I myself am confused on how to tackle this problem, I’ve been coding for a while now and didnt resort to posting on devforum, but here I am. I usually find a way around problems like these, but this issue has me completely dumbfounded.
Looking at your local script, I first noticed that you wrote “ml.OnClientInvoke:Connect(function()” when it should be “ml.OnClientInvoke = function()”.
Let me know how it goes.
EDIT: This is just a personal opinion kind of thing but highly against using InvokeClient() or generally the Server->Client->Server model which is shown here. Unless yeah the code genuinely needs this, would just say be careful. You can read more about it here: RemoteFunction | Documentation - Roblox Creator Hub
Thanks for your help! As I assumed, this didnt work. Actually, it’s now having an issue on the line in the server script where i invoke the client.
Also in response to your opinion, how would you suppose I approach this problem without the use of InvokeClient? I’m always down to learn from my mistakes
Does the error have to do with the fact that a LocalScript has insufficient permission?
If yes;
To fix this issue, all you have to do is make sure that the execution of the boolean’s change happens on a ServerScript. One easy way to do this is to use a ‘Client → Server’ RemoteEvent. The LocalScript will use the function :FireServer() to trigger any server-sided script that has a function connected to the RemoteEvent you are firing. To receive this trigger, you have to make a path to the RemoteEvent and use .OnServerEvent. Here is an example utilizing your code;
LocalScript
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local touchLock = game.Workspace:WaitForChild("Part") --//Part that you want to touch to enable the MouseLock
touchLock.Touched:Connect(function()
ReplicatedStorage.MouseLock:FireServer(player) --//This will fire the server-sided scripts, pass 'player' as a parameter to identify
end)
ServerScript
ReplicatedStorage.MouseLock.OnServerEvent:Connect(function(player) --//This will receive the trigger and the peramater
player.DevEnableMouseLock = true --//Using the parameter to identify which player it is
print("Locked " .. player.Name .. "'s mouse.")
end)
If no;
I suggest adding more to your topic. Showing what type of warning you get would be useful to see as well.
Hope this helped to clarify. Let me know if I missed anything.
Why are you using a RemoteFunction? If you don’t need to return anything, don’t use it. It takes up way more memory and is susceptible to yield attacks where they never return anything and your script is stuck forever. Just use RemoteEvents and :FireClient. In your code, you also do not reference the player.
Script:
-- Script is in serverscriptservice
local Players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local ld = workspace.LockDetect
local mouseLock = rs.MouseLock
ld.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
mouseLock:FireClient(player)
end
end)
LocalScript:
-- local script in starterplayer [i didnt know where to put it, suggestions would be appreciated]
local rep = game:GetService("ReplicatedStorage")
local ml = rep.MouseLock
local sps = script.Parent
ml.OnClientEvent:Connect(function() -- issue sometimes comes on this line, again not specified
print("??")
sps.EnableMouseLockOption = true
end)
I’m a fairly new programmer, I had no idea you could use remoteEvents to communicate between server to client. i was only familiar with client to server. After a bit of debugging, i got it working.
Again, thanks alot! I learned something new today ^^
I’d like to ask one more thing: Enabling the mouse lock option doesnt work for some reason. originally the player is supposed to not be able to use shiftlock until the part is touched. but even after the part is touched and the boolean is set to true, the shiftlock wont activate. what alternatives do you suggest i take to get around this problem?
-- local script in starterplayer [i didnt know where to put it, suggestions would be appreciated]
local rep = game:GetService("ReplicatedStorage")
local ml = rep.MouseLock
local sps = game:GetService('StarterPlayer')
ml.OnClientEvent:Connect(function() -- issue sometimes comes on this line, again not specified
print("??")
sps.EnableMouseLockOption = true
end)
thanks for the reply. this script didn’t solve my problem, but I don’t want this post to go off-topic. I’ll try to figure a way somehow. Again, thanks alot!