How to make a part the spawns banana pills everytime touched

Greetings Developers, I am a very new coder and i want to learn how to make a part that spawns banana peels every time a player touches the part. I am a beginner scripter so i do not know how make the code. So if you wouldn’t mind if you could teach me a few coding stuff that autuh be helpful to me.

BTW: I heard this was the best way to learn was by asking way experienced devs to teach me how to make stuff and understanding the code that they tell me and experimenting with the stuff i learnt so yeah.

Thank You For Your Time!

1 Like

These are events, they’re custom functions made by roblox. One of these custom functions listed is the .Touched event.

This an example usage


-- Script in ServerScriptService
local part = workspace.Part

local function onPartTouched(object)
	print("Part was touched by", object:GetFullName())
end

part.Touched:Connect(onPartTouched)

The best way to learn is starting with smaller projects and learning the basic fundamentals first. You can either do this by tutorials, people around you, or one of the ways I’d recommend which is Roblox Documentation.

Early on in the reading ^ you’ll learn how to make things happen when a part is touched and much more.

I’d start from the beginning but if you just want to learn right now how to make a touch event do something when a player touches it than look at this ^

Also if you know someone who knows how to script, I’d say you should livestream them the guide and walk it step by step since roblox doesn’t really use user friendly terminology or explanations in the guide.

2 Likes

You can reference the part in a script using

local part = game.workspace.Part

You then want to connect a hit function to this part with roblox’s built in events, and also a debounce so this doesn’t run too many times.

local Players = game:GetService("Player") -- Roblox provides services, we use this to get the player that touched the button
local ServerStorage = game:GetService("ServerStorage") -- Another service, you can find this service on the right side of your screen on the explorer tab
local db = false -- Our debounce
part.Touched:Connect(function(hit) -- A touched event, you can look up roblox documentation on touched events
	local player = Players:GetPlayerFromCharacter(part.Parent) -- Get the player
	if player and player.Character then -- Check if it's a player and their character
		if db then return end -- end the function if our debounce is true
		db = true -- make our debounce true so that we can't start this function again too quickly
		local bananaPill = ServerStorage.BananaPill:Clone() -- Make sure you put a bannapill model in the ServerStorage with this exact name "BananaPill"
		bananaPill.Parent = workspace -- Put this new clone of the model in the game
		bananaPill.Anchored = false -- This allows gravity to work and it can fall
		bananaPill.CFrame = part.CFrame * CFrame.new(0,10,0) -- This move it up 10 studs, the first 0 represents the x plane, the 10 represents the y plane, and the second 0 represents the z plane,
		task.wait(1) -- this indicates how fast you can press the button
		db = false -- Make our debounce false again so this script can run. If our debounce is true we made the whole script end
	end
end)
1 Like

TYSM, i learnt stuff i did not even know how they work, thank you!

thank you i was able to read the code and understand how it works