I'm gonna try and cover everything, here. So uh, here we go.
So, let's start with your local script
local repstorage = game:GetService("ReplicatedStorage")
local testevent = repstorage:FindFirstChild("test")
mouse = game.Players.LocalPlayer:GetMouse()
function onF()
game.ReplicatedStorage.test:FireServer()
end
game.Workspace.Baseplate.Touched:Connect(onF())
You have about 2 unused variables, repstorage, and testevent. (Assuming you're using the mouse for something else in the script)
If you don't use them, why have them?
I think you got variables mixed up, so here: "A variable is a name you can use to hold a value. In Lua, a variable can have any Data Types, such as a Number or a String."
From: https://developer.roblox.com/en-us/articles/Variables
And when you're running a function from a connect it would be :Connect(onF) not :Connect(OnF())
So the correct script here would be
local repstorage = game:GetService("ReplicatedStorage")
local testevent = repstorage:FindFirstChild("test")
function onF()
testevent:FireServer()
end
game.Workspace.Baseplate.Touched:Connect(onF)
Now, your script/server script
local repstorage = game:GetService("ReplicatedStorage")
local testevent = repstorage:FindFirstChild("test")
game.ReplicatedStorage.test.OnServerEvent:Connect(function()
print("test")
end)
Again, unused variables
The correct script here, would be:
game.ReplicatedStorage.test.OnServerEvent:Connect(function(plr)
print("test")
end)
You probably noticed that in one script, we used the variables, and in the other, we didn't. It doesn't matter for this case.
So, I noticed you didn't check if it was a player that touched it, since even if a part touches it, it would trigger the function.
So, how would you check if it was a player that touched it? Here.
game.Workspace.Baseplate.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum then
print("a player touched it!")
end
end)
Also, I've also noticed that you're detecting the touch on the client and firing an event to the server, you can skip the whole remote firing thing and detect the touched on the server instead.
So, if we place a script under baseplate it would look something like this:
script.Parent.Touched:Connect(function(hit)
local hum = hit.Parent.FindFirstChild("Humanoid")
if hum then
print("test")
end
end)
Also, I used a couple different scripting styles as you can notice. In the last one I didn't use a function, functions aren't a necessity but they just makes things neat and tidy. You can read more about functions here: https://developer.roblox.com/en-us/articles/Function
I know not the best of explaining for this question, but I tried my best lol. Goodluck!
2 comments
Now i'm getting: "- Remote event invocation queue exhausted for ReplicatedStorage.test; did you forget to implement OnServerEvent" Even though my Script looks correct.
Touched happens to fire repeatedly VERY fast, meaning your onF function is spammed. Thus the remote is repeatedly invoked too fast.Add a debounce to fix the same