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

Can anyone help me with remote events?

I'm really new to scripting, So this is probably gonna make people cringe.

What I've been doing: I've been trying to make a script that involves remote events.

What's the goal?: I'm trying to make it so that a brick (Or a piece of Toast, In my case) That spawns on the player when the E keybind is pressed.

What's the issue?: As you know, You need a local script to use a keybind, But I would like it so that everyone can see the brick (Or Toast).

What are my scripts so far?: Script A (LocalScript):

local object = game.Workspace.ToasT
local clonedObject = object:Clone()
local repStorage = game.GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")
local Key = 'E'
game:GetService("UserInputService").InputEnded:connect(function(input,IsTyping)
   if IsTyping then return end
   if input.KeyCode == Enum.KeyCode[Key] then
      remote:FireServer()
      wait(1)
   end
end)

Script B (Normal Script)

local object = game.Workspace.ToasT
local Position = game.StarterPlayer.StarterCharacter.Head.Toaster.Position
local ClonedObject = object:Clone()
local repStorage = game.GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")

remote.OnServerEvent:Connect(function(player)
	ClonedObject.Position = Vector3.new(Position)
end)

Please help.

4 comments

0

what does it shows in output?

0

Line 7, Bad Argument? (Script B)

0

Isnt that in script A?

1

because this: "if IsTyping then return end" looks suspicious sinse you had to return something. Put "if IsTyping then return nil end" just for sure.

1 answer

First, you cant call the player by "local player" when you are in server script, you have to use other methods, and when you clone something it exists only in memory, so you need to define its parent. You are just missing this:

ClonedObject.Parent = Workspace

Else, i can give you some tips in the code you wrote: you dont need to create the clone in local too (unless you want no lag feel on that action). Dont use a variable for Key sinse you will call it once or twice only. I would do it like this:

local object = game.Workspace.ToasT
local remote = game.ReplicatedStorage:WaitForChild("Remote")
game:GetService("UserInputService").InputEnded:connect(function(input,IsTyping)
	if IsTyping then return nil end
	if input.KeyCode == Enum.KeyCode.E then
		remote:FireServer("ActionName")
	end
end)

Server script:

local object = game.Workspace.ToasT
local ClonedObject = object:Clone()
local remote = game.ReplicatedStorage:WaitForChild("Remote")
 
remote.OnServerEvent:Connect(function(player, request)
	if request == "ActionName" then -- with this you can use this same remote to do other things later without having to create lots of remote events.
		ClonedObject.Position = Vector3.new(player.Character.Head.Toaster.Position)
	end 
end)

9 comments

0

Sorry but ROBLOX fixed, you can say game.Workspace, but if you want to shortform it, name it workspace instead.

0

I've tried your script, It doesn't seem to work, Or maybe Im just stupid. (Thanks for the Help BTW)

0

Nvm I got it :D! Thanks so much for the help! Heres the (WIP) Game: https://www.roblox.com/games/5085713537/Toaster-Island

0

I tried the game, looks very cool lol, but the E was not toasting, there is an error in the output saying "ToasT is not a valid member of workspace"

0

Also i just noticed that the code I wrote is trong too, there is one "end" out of place

0

Fixed now.

0

Hmm

0

I don't understand why its saying ToasT is not a valid member...

0

Probably that "local object = game.Workspace.ToasT", use the method game.Workspace:WaitForChild("ToasT"), it prevents the game to try to get the object when it is not loaded ingame yet, preventing error and script break.

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